Skip to content

New key event re-entrance deadlock fix#1

Merged
ArturKovacs merged 5 commits intonew-keyboardfrom
kbd-reentrance-fix
Jun 11, 2022
Merged

New key event re-entrance deadlock fix#1
ArturKovacs merged 5 commits intonew-keyboardfrom
kbd-reentrance-fix

Conversation

@ArturKovacs
Copy link
Copy Markdown
Owner

@ArturKovacs ArturKovacs commented Jan 12, 2022

An alternative fix for the deadlock in the new keyboard events. See original: rust-windowing#2146

The most important difference between this and the original fix is that this tries to ensure that keyboard events are sent to the user in the order they arrived from the OS, and NOT in the order they are completed processing. (The completion order is reversed during re-entrace)

Here's a concrete example:

  1. Messsage A is dispatched to winit
  2. Start processing A
  3. PeekMessage is called
  4. ---- Message B is dispatched to winit
  5. ---- Start processing B
  6. ---- PeekMessage is called (nothing happens)
  7. ---- Complete processing B
  8. Complete processing A
  • Tested on all platforms changed
  • Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

@fredizzimo
Copy link
Copy Markdown

Yes, now that you mention it, that was also something I was afraid of when I made the fix originally, but I decided to go on an try it anyway to see if it had any problems. And after using it for a few months, without any issues I forgot about that.

And this is with a QMK based keyboard, where I do a lot of tricks. For example I have a key where I press `, which is a dead key, but it actual does ` + space as fast as the usb protocol can handle it, so the space is sent 1ms after the `. I also have keys that send shfit + key and then automatically releases the shift key, also within 1 ms. And even keys that first release the shift key, to send and alt gr plus some key, and then press the shift again.

That said, there might be some very special case where things goes wrong, so your fix is probably better.

@ArturKovacs
Copy link
Copy Markdown
Owner Author

As far as I understand, having messages coming from the keyboard in quick succession cannot trigger re-entrance by itself, because those messages become "queued" messages (not dispatched by PeekMessage).

The problem arises when another thread sends a "nonqueued" message to the winit window. Because PeekMessage dispatches "nonqueued" messages.

@fredizzimo
Copy link
Copy Markdown

Yes, my point was more that it requires some very special circumstances to trigger any practical problems with the reversed event order. And that I would probably be more likely to notice them in my setup, than in other setups where the keyboard input is much slower, and PeekMessage wouldn't have a next key to read anyway.

Anyway, that was just an observation, fixing it correctly is obviously always better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants