-
-
Notifications
You must be signed in to change notification settings - Fork 947
Improper scoping keeps the thread waiting #861
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
Comments
Can you please elaborate on what you mean with "improper scope"? |
Currently: Scenario -2: Suppose one thread is already working on connecting the socket. During that time, another thread enters the try block acquiring a lock on semaphore and waiting on 'lock(this)' ---- This exhausts a semaphore slot during the entire time when the previous thread is working on establishing connection, In contrast, the new arrangement, semaphore is acquired just before the connection attempt. This seems to be optimized, |
Here is the PR link: https://github.com/sshnet/SSH.NET/pull/860/files |
As a general comment, please note that AuthenticationConnection is a static/shared, while Connect is locking on specific object instance (this). In scenario 1 the semaphore will be released in finally block. AFAIK the Session class' documentation doesn't state that it is thread safe. I cannot see a scenario in which this would be desirable/useful/needed. The current implementation tries to be thread safe, but does so at the expense of concurrency as you noted. While I do agree that your PR might help in some rare cases, I would propose to approach this problem from a different angle. I believe the semaphore was added to limit concurrent connection attempts to one server. Concerns such as this are much better handled by dedicated libraries, a notable example being Polly. So instead of imposing a global concurrency limit in a generic library (SSH.NET), this should be handled in the specific application having specific problems. Of course any such change would need to be thoroughly discussed as it could break some applications. |
The AuthenticationConnection semaphore has been removed in #1304 |
This issue has been fixed in the 2024.0.0 version. |
File : Session.cs
Method: Connect()
Due to improper scope AuthenticationConnection semaphore lock is acquired more than once when connect() method is called more than once while a connect operation is still in progress. It exhausts the semaphore slot and keeps the other Connect operations waiting until one finishes.
The text was updated successfully, but these errors were encountered: