Key callback randomly called outside of glfwPollEvents() (it seems like)

I have a simple wrapper around key events. I have a vector that I populate in the key callback with whatever key event happened, I then check it in the middle of the frame for inputs, and clear it at the end of the frame. My main loop looks something like this:

while(!glfwWindowShouldClose(w)) {
    glfwPollEvents();

    if (Input::isKeyJustPressed(GLFW_KEY_G)) printf("G\n");

    // Drawing...

    Input::clear();
}

I record key events in the key callback like this:

void Input::keyCallback(
	GLFWwindow* window, int key, int scancode, int action, int mods)
{
	keyKeyboardEvents.push_back({key, scancode, action, mods});
	printf("-------------------------------------\n");
	printf("Recording key: %d, action: %d\n", key, action);
}

And in Input::clear() I do:

keyKeyboardEvents.clear();

For some reason Input::isKeyJustPressed(GLFW_KEY_G) returns true around 1 out of 10 times. If I populate everywhere with prints I get this situation:

Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
-------------------------------------
Recording key: 71, action: 0
Clearing key: 71, action: 0
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71

As you can see - somehow the event is getting erased from the vector before it is checked, which doesn’t make sense, considering an order of operations in my code. The only reasonable suspicion I have is that callback somehow is not getting called in glfwPollEvents().

And once in a time I get expected behavior, where order makes sense(around once in every 5-20 presses):

Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
-------------------------------------
Recording key: 71, action: 1
Checking for key: 71
G
Clearing key: 71, action: 1
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71

The strangest thing is that I used this little wrapper in my other project on my other pc, and it worked correctly. I literally look at my old project code where everything worked nicely exactly like this. Unfortunately, I can’t build this old project on my current machine to check if the issue persists, and it is specific to this pc, because its too complicated.

My current machine: Minimal Debian 12 Bookworm running X server.

Not an glfw issue, found the mistake - I accidentally called glfwPollEvents() one more time in another place.

When you look at the documentation for glfwPollEvents it states this:

Do not assume that callbacks you set will only be called in response to event processing functions like this one. While it is necessary to poll for events, window systems that require GLFW to register callbacks of its own can pass events to GLFW in response to many window system function calls. GLFW will pass those events on to the application callbacks before returning.

It’s good that you found your solution, but unless I read the documentation wrong, it is not a guarantee that on all platforms the callback events will only be called during a call to glfwPollEvents. They could be called immediately when they happen (for example in Emscripten, that is what happens).