hidapi/windows: thread local error messages #688
No reviewers
Labels
No labels
3rdparty
Android
API
Author does not respond
bsd
bug
build system/CI
ci-virtual-device
CMake
Contributions Welcome
Core
dependencies
device-specific
documentation
don't_merge
duplicate
enhancement
Example
Haiku
hidraw
hotplug
info required
invalid
libusb
macOS
Misc
Not an issue
question
Release
Test
Test GUI
Windows
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-libusb/hidapi!688
Loading…
Reference in a new issue
No description provided.
Delete branch "thread-local-error-messages"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I've implemented thread local error messages on Windows to fix the double free crash when you have a Read and a Write happening in 2 different threads on the same device or when there is a thread for Read/Write for each device.
Need testing with multiple devices per thread since I only have 1 device per thread in my setup.
While this is an interesting idea to solve the current issue, there is unresolved issue with it:
If you're to use the device in different threads, you'll get several thread_local variables, but hid_close will free only one of them. There is a mem-leak by design here.
Oh you're right, I'm working on a fix.
I've added a global list which contains the thread local allocations. When a thread wants to allocate memory for a thread local variable, it adds it to this list. The list is cleared as necessary. Accesses to the list are protected with a critical section for thread safety.
The only scenarios this global list is accessed are :
One caveat :
I've implemented my suggestion for the caveat I mentioned.
Thanks for the implementation!
I totaly get your motivation for this change, but I have in mind a few doubts about this:
I was even thinkg about an alternative implementation for this - instead of having a gloal list of thread-local vairables - have a list of per-device error_strings linked to a current thread-id. That should be a bit more simpler.
Another alternative is to introduce a
hid_read_errorAPI function, which would give only hid_read and hid_read_timeout errors - that implementation should be the most trivial on any platform, but breaks backward compatibility a bit...While this is true, there is currently no way to have async read and write on a device without having to modify hidapi directly because of the current error message implementation. Right now you have to process them one by one or you risk a crash/undefined behavior. Having some thread safety guarantees is not a bad thing either. (the user still have to do their part, for example, the user must call hid_exit only when all threads are done using hidapi)
Understandable!
Shouldn't be too hard as long as we find an equivalent to DllMain. I could put the tls code in its own files to avoid code duplication.
I made an attempt here (not tested!) to see what it would look like. It's not much simplier imo (you have tls_get instead of tls_register mostly). And you have a potential lock contention each time error function are called (which is every times to set the error to null).
Yeah, it's unfortunate.
Edit : maybe this could be implemented without breaking backward compatibility by having a function you call to enable separate error for read and write ? But that could be confusing for the user...
At the end of the day, you're the one who has the say. If you want to take a cautious approach, you could also make a separate branch (like for the connection callback) to check the stability over time and for those who need this.
Pull request closed