Bug 53256 - Race in System.Net.EndPointListener constructor results in lock (null) {}
Summary: Race in System.Net.EndPointListener constructor results in lock (null) {}
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-03-11 01:29 UTC by Matthew Evans
Modified: 2017-09-08 23:21 UTC (History)
2 users (show)

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


Attachments
Test Case (1.45 KB, application/octet-stream)
2017-03-11 01:29 UTC, Matthew Evans
Details


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:
Status:
RESOLVED FIXED

Description Matthew Evans 2017-03-11 01:29:44 UTC
Created attachment 20280 [details]
Test Case

System.Net.EndPointListener starts accepting connections on its Socket before initializing the `unregistered` field (https://github.com/mono/mono/blob/mono-4.8.0.495/mcs/class/System/System.Net/EndPointListener.cs#L70). If a connection is accepted before the field is initialized, an exception will be thrown here https://github.com/mono/mono/blob/mono-4.8.0.495/mcs/class/System/System.Net/EndPointListener.cs#L125 when it tries to acquire a lock on null.

Moving initialization of `unregistered` (and probably `prefixes`, for good measure) before calling Accept should solve this.

Attached test case creates EndPointListeners and tries to connect to them in a tight loop. On my machine it throws after a few seconds.

$ uname -a
Linux monobuilder-48-01 3.13.0-110-generic #157-Ubuntu SMP Mon Feb 20 11:54:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
$ mono -V
Mono JIT compiler version 4.8.0 (Stable 4.8.0.495/e4a3cf3 Wed Feb 22 18:30:58 UTC 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug
	LLVM:          supported, not enabled.
	GC:            sgen
$ mcs --version
Mono C# compiler version 4.8.0.0
$ mcs Program.cs
$ mono Program.exe

Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: obj
  at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_monitor_enter_v4 (object,intptr)
  at System.Net.EndPointListener.ProcessAccept (System.Net.Sockets.SocketAsyncEventArgs args) [0x00070] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.EndPointListener.OnAccept (System.Object sender, System.Net.Sockets.SocketAsyncEventArgs e) [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) [0x00014] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncEventArgs.Complete () [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.Socket.<AcceptAsyncCallback>m__0 (System.IAsyncResult ares) [0x000a0] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncResult+<Complete>c__AnonStorey0.<>m__0 (System.Object _) [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00019] in <f712f98eb8e445c8918edaf595bbe465>:0
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00096] in <f712f98eb8e445c8918edaf595bbe465>:0
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <f712f98eb8e445c8918edaf595bbe465>:0

Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: obj
  at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_monitor_enter_v4 (object,intptr)
  at System.Net.EndPointListener.ProcessAccept (System.Net.Sockets.SocketAsyncEventArgs args) [0x00070] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.EndPointListener.OnAccept (System.Object sender, System.Net.Sockets.SocketAsyncEventArgs e) [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) [0x00014] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncEventArgs.Complete () [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.Socket.<AcceptAsyncCallback>m__0 (System.IAsyncResult ares) [0x000a0] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncResult+<Complete>c__AnonStorey0.<>m__0 (System.Object _) [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00019] in <f712f98eb8e445c8918edaf595bbe465>:0
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00096] in <f712f98eb8e445c8918edaf595bbe465>:0
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <f712f98eb8e445c8918edaf595bbe465>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentNullException: Value cannot be null.
Parameter name: obj
  at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_monitor_enter_v4 (object,intptr)
  at System.Net.EndPointListener.ProcessAccept (System.Net.Sockets.SocketAsyncEventArgs args) [0x00070] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.EndPointListener.OnAccept (System.Object sender, System.Net.Sockets.SocketAsyncEventArgs e) [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) [0x00014] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncEventArgs.Complete () [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.Socket.<AcceptAsyncCallback>m__0 (System.IAsyncResult ares) [0x000a0] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Net.Sockets.SocketAsyncResult+<Complete>c__AnonStorey0.<>m__0 (System.Object _) [0x00000] in <5641e4edad4f4464ba58c620a7b8ea48>:0
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00019] in <f712f98eb8e445c8918edaf595bbe465>:0
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00096] in <f712f98eb8e445c8918edaf595bbe465>:0
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <f712f98eb8e445c8918edaf595bbe465>:0