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

NetworkStream ReadAsync throws ObjectDisposedException #1327

Closed
faucetcollector opened this issue Feb 8, 2022 · 2 comments
Closed

NetworkStream ReadAsync throws ObjectDisposedException #1327

faucetcollector opened this issue Feb 8, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@faucetcollector
Copy link

Describe the bug
When a read timeout occurs in the NetworkStream.ReadAsync it will Disconnect the Socket. But in the Exception message it will use the ReadTimeout property, which reads the Socket.ReceiveTimeout property. This will throw an ObjectDisposedException because it was just disposed by calling Disconnect(); in the line before.

} catch (OperationCanceledException ex) {
	Disconnect ();
	if (timeout.IsCancellationRequested)
		throw new TimeoutException ($"Operation timed out after {ReadTimeout} milliseconds", ex);
	throw;
}

Suggested fix is to store the ReadTimeout in a local variable and use that in the exception message.

Platform (please complete the following information):

  • OS: Windows
  • .NET Framework: net6
  • MailKit Version: 3.1.1

Exception

Exception: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
   at System.Net.Sockets.Socket.ThrowObjectDisposedException()
   at System.Net.Sockets.Socket.GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
   at System.Net.Sockets.Socket.get_ReceiveTimeout()
   at MailKit.Net.NetworkStream.get_ReadTimeout()
   at MailKit.Net.NetworkStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at MailKit.Net.Smtp.SmtpStream.ReadAheadAsync(Boolean doAsync, CancellationToken cancellationToken)
   at MailKit.Net.Smtp.SmtpStream.ReadResponseAsync(Boolean doAsync, CancellationToken cancellationToken)
   at MailKit.Net.Smtp.SmtpClient.SendCommandAsync(String command, Boolean doAsync, CancellationToken cancellationToken)
@faucetcollector
Copy link
Author

Looks like the same issue applies in the WriteAsync method with the WriteTimeout property in the Exception message.

@jstedfast
Copy link
Owner

Thanks for reporting this!

jstedfast added a commit that referenced this issue Feb 8, 2022
@jstedfast jstedfast added the bug Something isn't working label Feb 9, 2022
Elanis pushed a commit to Elanis/portfolio that referenced this issue Dec 13, 2022
Bumps [MailKit](https://github.com/jstedfast/MailKit) from 3.1.1 to 3.2.0.
<details>
<summary>Changelog</summary>

*Sourced from [MailKit's changelog](https://github.com/jstedfast/MailKit/blob/master/ReleaseNotes.md).*

> ### MailKit 3.2.0 (2022-03-26)
>
> * Do not use ApplicationProtocols with SSL. (issue [#1352](jstedfast/MailKit#1352))
> * Updated GMail, Yahoo, and Outlook.com certificates.
> * Lazy-initialize MessageSummary.Keywords. This reduces memory usage when the client isn't requesting Flags/Keywords.
> * Hard-cache some IMAP FETCH-related tokens in order to relieve GC pressure for commands like FETCH where there can
>   be a LOT of responses containing the same tokens over and over again.
> * Converted some IMAP async Task methods to use ValueTask to reduce GC pressure.
> * Reduced string allocations in the IMAP logic by avoiding use of ToUpperInvariant().
> * Added non-async implementations for ImapStream APIs to be used by the synchronous public APIs to avoid some async overhead.
> * Reduce MemoryStream (and thus byte[]) allocations by using a new ByteArrayBuilder.
> * Rewrote the IMAP CAPABILITY parser to avoid allocating strings.
> * Fixed some cases where IMAP NIL tokens were not compared case insensitively.
> * Always include the VERSION block in NTLM messages. (issue [#1340](jstedfast/MailKit#1340))
> * Target .NET Framework v4.6.1 instead of v4.6 to match the changes in MimeKit.
> * Capture the Socket timeout value in Read/WriteAsync() to have it in case of exceptions.
>   (issue [#1327](jstedfast/MailKit#1327))
</details>
<details>
<summary>Commits</summary>

- [`77eb925`](jstedfast/MailKit@77eb925) Bumped version to 3.2.0
- [`1af5110`](jstedfast/MailKit@1af5110) Do not use ApplicationProtocols with SSL
- [`73c6f67`](jstedfast/MailKit@73c6f67) Updated GMail and Yahoo certificates
- [`c3a48ac`](jstedfast/MailKit@c3a48ac) Bump nunit from 3.13.2 to 3.13.3 ([#1350](jstedfast/MailKit#1350))
- [`a736d88`](jstedfast/MailKit@a736d88) Lazy-initialize MessageSummary.Keywords
- [`ab1e087`](jstedfast/MailKit@ab1e087) Use token.Value.ToString() in case the token is a char token
- [`16adde1`](jstedfast/MailKit@16adde1) Updated GMail certificates
- [`d284a2d`](jstedfast/MailKit@d284a2d) Hard-cache some IMAP FETCH-related tokens
- [`cfe6dba`](jstedfast/MailKit@cfe6dba) Always include the VERSION block in NTLM messages.
- [`cfa66e4`](jstedfast/MailKit@cfa66e4) Convert more async IMAP methods to ValueTask
- Additional commits viewable in [compare view](jstedfast/MailKit@3.1.1...3.2.0)
</details>

<br />

Reviewed-on: https://gitea.dysnomia.studio/elanis/portfolio/pulls/31
Co-authored-by: elanis <[email protected]>
Co-committed-by: elanis <[email protected]>
Elanis added a commit to Dysnomia-Studio/dysnomia-website that referenced this issue Jul 14, 2023
Bumps [MailKit](https://github.com/jstedfast/MailKit) from 3.1.1 to 3.2.0.
<details>
<summary>Changelog</summary>

*Sourced from [MailKit's changelog](https://github.com/jstedfast/MailKit/blob/master/ReleaseNotes.md).*

> ### MailKit 3.2.0 (2022-03-26)
>
> * Do not use ApplicationProtocols with SSL. (issue [#1352](jstedfast/MailKit#1352))
> * Updated GMail, Yahoo, and Outlook.com certificates.
> * Lazy-initialize MessageSummary.Keywords. This reduces memory usage when the client isn't requesting Flags/Keywords.
> * Hard-cache some IMAP FETCH-related tokens in order to relieve GC pressure for commands like FETCH where there can
>   be a LOT of responses containing the same tokens over and over again.
> * Converted some IMAP async Task methods to use ValueTask to reduce GC pressure.
> * Reduced string allocations in the IMAP logic by avoiding use of ToUpperInvariant().
> * Added non-async implementations for ImapStream APIs to be used by the synchronous public APIs to avoid some async overhead.
> * Reduce MemoryStream (and thus byte[]) allocations by using a new ByteArrayBuilder.
> * Rewrote the IMAP CAPABILITY parser to avoid allocating strings.
> * Fixed some cases where IMAP NIL tokens were not compared case insensitively.
> * Always include the VERSION block in NTLM messages. (issue [#1340](jstedfast/MailKit#1340))
> * Target .NET Framework v4.6.1 instead of v4.6 to match the changes in MimeKit.
> * Capture the Socket timeout value in Read/WriteAsync() to have it in case of exceptions.
>   (issue [#1327](jstedfast/MailKit#1327))
</details>
<details>
<summary>Commits</summary>

- [`77eb925`](jstedfast/MailKit@77eb925) Bumped version to 3.2.0
- [`1af5110`](jstedfast/MailKit@1af5110) Do not use ApplicationProtocols with SSL
- [`73c6f67`](jstedfast/MailKit@73c6f67) Updated GMail and Yahoo certificates
- [`c3a48ac`](jstedfast/MailKit@c3a48ac) Bump nunit from 3.13.2 to 3.13.3 ([#1350](jstedfast/MailKit#1350))
- [`a736d88`](jstedfast/MailKit@a736d88) Lazy-initialize MessageSummary.Keywords
- [`ab1e087`](jstedfast/MailKit@ab1e087) Use token.Value.ToString() in case the token is a char token
- [`16adde1`](jstedfast/MailKit@16adde1) Updated GMail certificates
- [`d284a2d`](jstedfast/MailKit@d284a2d) Hard-cache some IMAP FETCH-related tokens
- [`cfe6dba`](jstedfast/MailKit@cfe6dba) Always include the VERSION block in NTLM messages.
- [`cfa66e4`](jstedfast/MailKit@cfa66e4) Convert more async IMAP methods to ValueTask
- Additional commits viewable in [compare view](jstedfast/MailKit@3.1.1...3.2.0)
</details>

<br />

Co-authored-by: Elanis <[email protected]>
Reviewed-on: https://gitea.dysnomia.studio/elanis/dysnomia-website/pulls/35
Co-authored-by: elanis <[email protected]>
Co-committed-by: elanis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants