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

ObjectDisposedException in PodeRequest During SslStream Operation #1459

Open
mdaneri opened this issue Nov 29, 2024 · 0 comments · May be fixed by #1460
Open

ObjectDisposedException in PodeRequest During SslStream Operation #1459

mdaneri opened this issue Nov 29, 2024 · 0 comments · May be fixed by #1460
Labels

Comments

@mdaneri
Copy link
Contributor

mdaneri commented Nov 29, 2024

Summary

An ObjectDisposedException is thrown intermittently during SSL/TLS operations when running a Pode server using the Start-PodeServer function. The exception occurs in the PodeRequest.Receive method while handling an SslStream.

Steps to Reproduce

  1. Start a Pode server with the following configuration:

    Start-PodeServer -Threads 6 {
        Add-PodeEndpoint -Address "*" -Port "8080" -Protocol "Https" -SelfSigned
    
        New-PodeLoggingMethod -File -Name "requests" | Enable-PodeRequestLogging
        New-PodeLoggingMethod -File -Name "errors" | Enable-PodeErrorLogging
    
        Add-PodeRoute -Method Get -Path "/" -ScriptBlock {
            Write-PodeTextResponse -Value "Test"
        }
    }
  2. Send multiple HTTPS requests to the server simultaneously (e.g., using a browser or a load-testing tool).

Observed Behavior

The server intermittently logs the following error:

[Error] ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
   at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
   at System.Net.Security.SslState.get_SecureStream()
   at System.Net.Security.SslStream.EndRead(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncTrimPromise`1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Pode.PodeRequest.<Receive>d__83.MoveNext()

Expected Behavior

The Pode server should process HTTPS requests without encountering an ObjectDisposedException.

Potential Cause

The issue appears to be related to concurrent access or premature disposal of the SslStream instance. It may occur under high concurrency due to:

  1. Multiple threads attempting to access the same SslStream object.
  2. The stream being disposed of while another operation is still in progress.

Relevant Code

  • The issue is observed in the PodeRequest.Receive method, specifically during operations involving SslStream.
  • The problem may be triggered by the StreamLock semaphore not adequately preventing concurrent access, or the disposal logic in the Dispose method being invoked prematurely.

Environment

  • Pode version: 2.11.x
  • .NET runtime: NetStandard2.0
  • OS: Windows
  • PowerShell version: 5.1

Possible Solutions

  1. Ensure Thread-Safe Access:

    • Verify that all operations on the SslStream are wrapped in thread-safe mechanisms, such as SemaphoreSlim.
  2. Improve Stream Disposal Logic:

    • Ensure the Dispose method in PodeRequest checks the IsOpen state and prevents disposal of an active stream.
    • Delay the disposal of SslStream until all pending operations are completed.
  3. Add Defensive Checks:

    • Before performing read/write operations on SslStream, verify that it has not been disposed of.
  4. Suppress Expected Exceptions in Logging:

    • If the error is unavoidable in certain edge cases, log the error at a Debug level instead of an Error level to avoid alarming users unnecessarily.

Impact

  • Affects HTTPS functionality in Pode servers under high-concurrency scenarios.
  • Potentially degrades server stability due to repeated exceptions.

Workaround

  • Reduce the number of threads using the -Threads parameter to limit concurrent operations.
  • Avoid high-concurrency loads until a fix is implemented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

Successfully merging a pull request may close this issue.

1 participant