Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock when hid_read and hid_write are called at the same time #2179

Merged
merged 6 commits into from
Nov 20, 2019

Conversation

codecat
Copy link
Contributor

@codecat codecat commented Jun 18, 2019

This fixes a problem where the controller thread would deadlock when hid_read_timeout and hid_write were called at the same time.

As discussed on Zulip, this was fixed by removing HidReader in favor of using the poll() method so that HID reading runs on the same thread as writing.

@Pegasus-RPG
Copy link
Member

Looks fine to me at first glance, but I don't remember why we added the HIDReader in the first place. @rryan do you?

@codecat
Copy link
Contributor Author

codecat commented Jun 18, 2019

Maybe the thread priority? I don't know what the thread priority is for the controller though.

@codecat
Copy link
Contributor Author

codecat commented Jun 18, 2019

Sidenote, I dunno if the AppVeyor build is supposed to fail, but I'm not sure why it's failing.

@Be-ing
Copy link
Contributor

Be-ing commented Jun 19, 2019

Maybe the thread priority? I don't know what the thread priority is for the controller though.

The ControllerManager constructor starts the controller thread with QThread::HighPriority like the HidReader thread was.

Sidenote, I dunno if the AppVeyor build is supposed to fail, but I'm not sure why it's failing.

I think it timed out. I manually restarted it.

@Be-ing
Copy link
Contributor

Be-ing commented Jun 19, 2019

Thank you for investigating this issue and working on it. Could you sign our Contributor Agreement before this is merged? Please leave a comment when you have signed it.

@codecat
Copy link
Contributor Author

codecat commented Jun 19, 2019

I’ve signed the contributor agreement.

@WaylonR
Copy link
Contributor

WaylonR commented Jun 20, 2019

Ummm.. using the latest artifacts from this pull, Something is wrong.. the midi to screen response times are incredibly sluggish. I turn my eq knobs, and takes a second to turn inside mixxx, and doesn't show inbetween states. one second its at 12oclock, the next, where I set it to.

same with playing, and lol. scratching is not happening.

Midi controller - Pioneer DDJ-SR2

@Be-ing
Copy link
Contributor

Be-ing commented Jun 20, 2019

@WaylonR this branch makes no changes for MIDI controllers.

@codecat
Copy link
Contributor Author

codecat commented Jun 20, 2019

Does each controller get its own thread?

@Be-ing
Copy link
Contributor

Be-ing commented Jun 20, 2019

... unless you're using a MIDI controller and an HID controller at the same time, in which case the blocking read for the HID controller would affect the MIDI controller too.

@Be-ing
Copy link
Contributor

Be-ing commented Jun 20, 2019

Does each controller get its own thread?

Apparently not. Perhaps that would be a good idea. On the other hand, maybe having multiple threads polling at a high frequency isn't a great idea.

@WaylonR
Copy link
Contributor

WaylonR commented Jun 20, 2019

Moved to another pr, #2172 and, the problem i described above, disappeared.

This does affect midi controllers.

@codecat
Copy link
Contributor Author

codecat commented Jun 20, 2019

Do you have both HID and Midi controllers enabled? Because that could be why.

@codecat
Copy link
Contributor Author

codecat commented Jun 20, 2019

I committed a change making it non-blocking now. It works well for me. @WaylonR, can you try this PR again?

@codecat
Copy link
Contributor Author

codecat commented Jun 20, 2019

Should be ok now! 👌

@codecat
Copy link
Contributor Author

codecat commented Jun 21, 2019

Committed those changes! 👍

@codecat
Copy link
Contributor Author

codecat commented Jun 25, 2019

Not for me - in my tests earlier it will return 0 after roughly 70 iterations.

@codecat
Copy link
Contributor Author

codecat commented Jul 2, 2019

So - what's the conclusion? What do you guys think should be done here? I don't have any other HID-based controller I can test with so it's a bit hard for me to make any more helpful judgements, I think.

@uklotzde uklotzde added this to the 2.3.0 milestone Oct 20, 2019
@uklotzde
Copy link
Contributor

@Be-ing Ready to merge? I don't have any HID devices for testing.

@codecat
Copy link
Contributor Author

codecat commented Nov 7, 2019

Would appreciate a merge, as I could really use this in the stable version of Mixxx. I can work around this issue by not writing anything to the device, but that's not a great solution.

For what it's worth, I've been using this with my controller for a long time and it has always worked well.

@Be-ing
Copy link
Contributor

Be-ing commented Nov 9, 2019

Sorry for going quiet on this. This still has not been tested with controllers that use multiple different input packets like the Kontrol S4 Mk2, which I am afraid this may break. Ideally, I would also like to verify that this works on Linux and macOS, but unfortunately not many contributors have HID controllers to test with.

I can work around this issue by not writing anything to the device, but that's not a great solution.

As discussed earlier on Zulip, the issue you encountered writing your script should only become a problem when rapidly writing HID output constantly. Mixxx should indeed not break when this happens, but on the other hand scripts shouldn't be doing that. Scripts should only send output when the state of Mixxx changes, not constantly on a timer.

@codecat
Copy link
Contributor Author

codecat commented Nov 9, 2019

Scripts should only send output when the state of Mixxx changes, not constantly on a timer.

That is what my script has been doing now, and it still locks up. I have it hooked up to stuff like VuMeter as well though which is probably called a lot.

@Be-ing
Copy link
Contributor

Be-ing commented Nov 11, 2019

I asked some users who have a Kontrol S4 Mk2 to test this. I am still nervous to merge this without confirmation that controllers that use multiple input packets still work.

That is what my script has been doing now, and it still locks up. I have it hooked up to stuff like VuMeter as well though which is probably called a lot.

Hmm, other HID scripts do that without a problem...

@codecat
Copy link
Contributor Author

codecat commented Nov 11, 2019

@nPrevail
Copy link

nPrevail commented Nov 13, 2019

I did some tests using the Traktor S4 MK2 with Mixxx. My laptop is a Windows 10 PC. Here's all the problems I found:

Audio tears/clipping.

No indication in the library that shows what tracks are currently playing. Can only see what's playing by looking at each individual deck.

Traktor playlists has no indication showing what tracks were previously played (lacks the checkmark box).

Crossfader assignment for each channel is off (Unable to turn on the crossfader cut)

Moving the platter while the track is stopped will jolt even after the platter slows down or isnt' moving.

Crossfader doesn't always cut off music, especially when doing fast and small cuts.

Error message:
Uncaught exception at line 1332 in file C:/Users/nnail/AppData/Local/Mixxx/controllers/Traktor-Kontrol-S4-MK2-hid-scripts.js: TypeError: Result of expression 'numArray[character]' [undefined] is not an object.
(Found this error while playing with beat jump and looping)

Laptop battery seemed to drain fast(?)

Jogwheel pitch bend needs more sensitivity (barely able to pitch bend spinning the platter by the edge)

Preview button sometimes replays the same track/fails to pause and replays the track again (too sensitive?) Seems like the signal is double inputted.

Preview button sometimes previews previous tracks that were heard.

Moog Ladder filter "hi" frequency doesn't completely kill the audio when applied.

@Be-ing
Copy link
Contributor

Be-ing commented Nov 14, 2019

Thank you for testing @nPrevail, but what we are interested regarding this pull request is only whether the controller works. Does every part of the controller work (as described on its wiki page) while using this? How long did you test for?

The script error you posted look like a bug in the mapping, which is a separate issue from the changes being discussed here. Any issues with the mapping you can discuss in the Zulip topic for that controller.

@nPrevail
Copy link

nPrevail commented Nov 14, 2019

Everything seemed to respond. I tried all the features. For some buttons, I wasn't sure what they did. I didn't understand what the Snap or Quantize button did (I know one changed the view from browser mode to DJ mode).

But the controller worked, the lights lit up, and HID responded. The only thing I wasn't able to fully test out were my audio master and booth outputs.

I used the controller for a couple of hours.

@Be-ing
Copy link
Contributor

Be-ing commented Nov 20, 2019

Thank you very much for testing @nPrevail. I'm not sure how this is working, but it is, so I'll go ahead and merge this now. I am guessing that for controllers with multiple input packets, it is a matter of chance which type of packet is received when Mixxx polls for an update, but the polling happens frequently enough that it doesn't matter that all packets aren't processed simultaneously.

@Be-ing Be-ing merged commit 5eb0594 into mixxxdj:master Nov 20, 2019
@nPrevail
Copy link

nPrevail commented Nov 20, 2019 via email

@codecat
Copy link
Contributor Author

codecat commented Nov 20, 2019

Nice!! I'm very happy this is merged. Thank you!

@daschuer
Copy link
Member

Hi @codecat, I like to put your name on the contributor list in the Mixxx about box. Is it OK If I use your real name?

@codecat
Copy link
Contributor Author

codecat commented Apr 23, 2020

Oh cool! Yes, Melissa is ok 🥰

@Be-ing
Copy link
Contributor

Be-ing commented Jul 27, 2020

@codecat it seems this introduced a regression with HID controllers on Linux that makes input laggy. Refer to the discussion in #2966.

Be-ing added a commit to Be-ing/mixxx that referenced this pull request Jul 27, 2020
Otherwise, it seems messages build up in a queue which produces
a dramatic lag on Linux when moving faders quickly. This
regression was introduced between Mixxx 2.2 and 2.3 beta, likely
by switching HidController to nonblocking polling in PR mixxxdj#2179.
Be-ing added a commit to Be-ing/mixxx that referenced this pull request Aug 31, 2020
Otherwise, it seems messages build up in a queue which produces
a dramatic lag on Linux when moving faders quickly. This
regression was introduced between Mixxx 2.2 and 2.3 beta, likely
by switching HidController to nonblocking polling in PR mixxxdj#2179.
ywwg pushed a commit to ywwg/mixxx that referenced this pull request Sep 3, 2020
Otherwise, it seems messages build up in a queue which produces
a dramatic lag on Linux when moving faders quickly. This
regression was introduced between Mixxx 2.2 and 2.3 beta, likely
by switching HidController to nonblocking polling in PR mixxxdj#2179.
JoergAtGithub added a commit to JoergAtGithub/mixxx that referenced this pull request Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants