Bug 57528 - [THIS WEEK] Nested streams using SslStream cause ObjectDisposedException when disposed
Summary: [THIS WEEK] Nested streams using SslStream cause ObjectDisposedException when...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC All
: --- normal
Target Milestone: Untriaged
Assignee: Martin Baulig
Depends on:
Reported: 2017-06-15 15:40 UTC by Peter Nelson
Modified: 2018-04-10 17:21 UTC (History)
4 users (show)

Tags: bugpool-archive
Is this bug a regression?: Yes
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Peter Nelson 2017-06-15 15:40:31 UTC
I'm using precompiled Mono JIT compiler version (2017-02/5077205 Thu May 25 09:19:47 UTC 2017)
and Mono JIT compiler version 3.2.8 (Debian 3.2.8+dfsg-10)

In mono, nested streams consuming a SslStream cause an ObjectDisposedException to occur on when disposed. I don't know when this was introduced as I have not been using any intermediate versions.

$ ./Test.exe www.google.com
Testing nested using() block

Unhandled Exception:
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
  at System.Net.Security.SslStream.CheckDisposed () [0x00008] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at System.Net.Security.SslStream.get_Impl () [0x00000] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at System.Net.Security.SslStream.Flush () [0x00000] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at System.IO.StreamWriter.Flush (System.Boolean flushStream, System.Boolean flushEncoder) [0x00094] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.IO.StreamWriter.Dispose (System.Boolean disposing) [0x00022] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.IO.TextWriter.Dispose () [0x00000] in <a07d6bf484a54da2861691df910339b1>:0 
  at TestScript.MainClass.Main (System.String[] args) [0x0006e] in <cfdd328f00084edba67b1f5750bf4c46>:0 

In mono 3.2.8, this test case works correctly with no exception:

$ ./Test.exe www.google.com
Testing nested using() block

Below is my test case code:

class MainClass
	public static void Main(string[] args)
		var t = new TcpClient();
		t.Connect(args[0], 443);
		var s = t.GetStream();
		var ssl = new SslStream(s, false);
		ssl.AuthenticateAsClient(args[0], null, System.Security.Authentication.SslProtocols.Tls, false);

		Console.WriteLine("Testing nested using() block");
		using (var sw = new StreamWriter(ssl))
		using (var sr = new StreamReader(ssl))
Comment 1 Marek Safar 2017-09-08 23:12:58 UTC
Full repro

using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;

class MainClass
	public static void Main (string [] args)
		var t = new TcpClient ();
		t.Connect ("www.google.com", 443);
		var s = t.GetStream ();
		var ssl = new SslStream (s, false);
		ssl.AuthenticateAsClient ("www.google.com", null, System.Security.Authentication.SslProtocols.Tls, false);

		Console.WriteLine ("Testing nested using() block");
		using (var sw = new StreamWriter (ssl))
		using (var sr = new StreamReader (ssl)) {
Comment 2 Martin Baulig 2017-09-13 20:09:03 UTC
Alright, this will be fixed shortly :-)

In the legacy implementation, all that SslStream.Flush() did was to flush the underlying stream.  In the current implementation, it actually does nothing because Write() already flushes the stream (which is an implementation requirement).

So I'm going to make SslStream.Flush() a no-op.

On a similar notice, SslStream.CanTimeout, CanRead and CanWrite should never throw (they currently do).
Comment 3 Martin Baulig 2017-09-13 22:34:01 UTC
Turns out that our implementation is actually incorrect - it should call Flush() on the underlying stream.
Comment 4 Martin Baulig 2017-09-13 23:47:48 UTC
Fixed https://github.com/mono/mono/pull/5569.
Comment 5 Peter Nelson 2018-04-10 11:11:41 UTC
I'm testing with mono 5.4.1 ((HEAD/e66d9ab Fri Jan 12 18:41:48 UTC 2018) which I believe contains this PR. With the same test case I still get an ObjectDisposedException.
Comment 6 Ludovic Henry 2018-04-10 17:21:36 UTC
Peter, the version you are testing doesn't contain the fix yet. Can you still reproduce with Mono 5.10 or later?