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

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


Attachments

Description Peter Nelson 2017-06-15 15:40:31 UTC
I'm using precompiled Mono JIT compiler version 5.0.1.1 (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 5.0.1.1, 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.

Note You need to log in before you can comment on or make changes to this bug.