posix/sockets: Prevent deadlock with multiple posix TCP sockets #21784
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.
Contribution description
When 2 posix TCP sockets in different threads are open and the first one is in accept() the mutex of the socket pool was not unlocked until data was received. When another thread calls a function which needs the socket pool mutex there was a deadlock. Unlocking the mutex after all operations on the socket pool are done, in particular BEFORE sock_tcp_accept() solves this.
I verified all function calls between my insertion and the later mutex unlock do not access the socket pool.
Testing procedure
Since this change is small I hope it is sufficient to describe the problem:
In one thread open a posix socket and accept(), this blocks until data is received.
In another thread call any other socket pool dependent posix/socket function, like socket().
Expected:
Second thread can create/... sockets.
Actual:
Deadlock, as the socket pool is still locked.
Issues/PRs references
None, just found this without creating an issue, while porting posix dependent code.