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

Connection: Fix unexpected server disconnection due to OpenSSL thread safety problems #9

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

botanegg
Copy link
Contributor

Its a dirty 15 minutes quick fix to show the idea

OpenSSL FAQ https://www.openssl.org/docs/faq.html said about only one thread should use a socket

Partially_Closes #7

@botanegg botanegg marked this pull request as draft February 20, 2023 09:18
@Krzmbrzl
Copy link
Member

The failing CI should be fixed by rebasing against current master branch.

@davidebeatrici what is the plan with this PR?

@davidebeatrici
Copy link
Member

I'm a bit concerned about locking in a function that may be called several times in a second... maybe we should let the decision up to the API user.

@Krzmbrzl
Copy link
Member

maybe we should let the decision up to the API user.

I vote strongly against that. If OpenSSL explicitly states that one should use single-thread access, then that's exactly what we should do.
I think the best approach would be to have the socket handling (reading and writing) in a single thread (that only does this) and then all processing happens in different threads and once they want to send something, they queue their message in some sort of queue that is processed by the socket thread sequentially.
If I'm not mistaken, socket operations can't be performed in parallel on hardware-level anyway.

@davidebeatrici
Copy link
Member

Well, that's what we do inside the library itself.

It's the example code that has to be fixed...

@botanegg
Copy link
Contributor Author

Sounds good, but I dont know about my time to implement Krzmbrzl approach

@Krzmbrzl
Copy link
Member

I'm wondering though: does this even have to be part of the public API? Individual connections and especially writing data to the respective sockets seems like something that should be abstracted away by the library, doesn't it?
If we remove it from the public interface, we can simply ensure that it is used properly internally and be done with it. If we leave it in the public API, then I guess the way to go would be to state that these functions must only ever by accessed from a single thread, but that is kinda ugly and it is likely that downstream users will mess it up 🤔

@davidebeatrici
Copy link
Member

Connections can be added to Peer for automatic management, but data still has to be sent through the specific connection's function.

@Krzmbrzl
Copy link
Member

Correct, but if we don't expose that to the end user, that's one thing less to worry about. Then we can manage the threading internally and keep the library users out of it.
And then we can figure out a way to nicely implement the worker+sender threads

@davidebeatrici davidebeatrici marked this pull request as ready for review February 23, 2024 22:53
… safety problems

From the OpenSSL FAQ (https://www.openssl.org/docs/faq.html):

"... an SSL connection cannot be used concurrently by multiple threads. This is true for most OpenSSL objects."
@davidebeatrici
Copy link
Member

While using the library for a new project I encountered the issue several times, thus I decided to rebase this and merge it right away.

My initial comment is still valid, but I vastly overestimated the cost of acquiring and releasing a mutex.

@botanegg The fix you proposed is simple and effective. @Krzmbrzl's idea would avoid the need for a mutex, but it adds quite some complexity and is actually not guaranteed to be the best mechanism in terms of performance.

@davidebeatrici davidebeatrici changed the title Fixed server disconnection due to OpenSSL thread safety problems Connection: Fix unexpected server disconnection due to OpenSSL thread safety problems Feb 23, 2024
@davidebeatrici davidebeatrici merged commit 4321355 into mumble-voip:master Feb 23, 2024
4 of 6 checks passed
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.

3 participants