Skip to content

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

Closed
SwarajKetan opened this issue Aug 15, 2021 · 6 comments
Closed

Improper scoping keeps the thread waiting #861

SwarajKetan opened this issue Aug 15, 2021 · 6 comments
Milestone

Comments

@SwarajKetan
Copy link

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.

@IgorMilavec
Copy link
Collaborator

Can you please elaborate on what you mean with "improper scope"?

@SwarajKetan
Copy link
Author

Currently:
Scenario -1: Suppose, A thread enters the try block. Then it acquires the lock. Suppose the the session is already connected. It returns releasing lock from finally block. ---- This exhausts a semaphore slot unnecessarily.

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,

@SwarajKetan
Copy link
Author

@IgorMilavec
Copy link
Collaborator

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.
Scenario 2 would only happen if Connect on the same Session instance would be called concurrently from two threads. And at least 4 threads would need to tango for this to have any impact (due to semaphore count being 3).

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.

@Rob-Hague
Copy link
Collaborator

The AuthenticationConnection semaphore has been removed in #1304

@WojciechNagorski
Copy link
Collaborator

This issue has been fixed in the 2024.0.0 version.

@WojciechNagorski WojciechNagorski added this to the 2024.0.0 milestone Feb 22, 2024
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

No branches or pull requests

4 participants