Fix flushed capture data being missed at shutdown #96
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR does a fair bit of refactoring of the Cynthion capture backend, in order to solve a quite simple problem.
The bug was that during shutdown, Packetry was tearing down the data transfer queue before sending the control request to stop capture. This meant that capture data which was flushed by the analyzer at stop time was lost. In most cases, this wasn't noticeable, but when using low-traffic devices (especially LS keyboards), a whole capture could be lost entirely, if it never produced the 512 bytes needed to fill a packet to the host.
Test case:
To fix this there were basically two options:
Do a bunch more fiddly work to integrate the start/stop control requests into the same async task as the data transfer queue handling, including re-implementing timeouts for them.
Spawn an additional worker thread for the data transfer queue processing, allowing the existing capture thread to continue to use nusb's synchronous API for control requests.
I spent a bunch of time trying to do option 1. It's ugly and difficult. So this PR implements option 2.
This choice also helps with the upcoming HITL test work, where we need to send additional control requests during capture, to manage the test device on the AUX port. Option 1 would have required these too to be integrated into the async task.
To simplify the HITL use case further, this PR also makes
CynthionHandle::start()
cloneself
to pass to the worker thread, rather than consuming it, meaning the test can keep that handle to make its additional requests with.