Bug 30043

Summary: Disposing a FileSystemWatcher object causes ArgumentOutOfRangeException
Product: [Mono] Runtime Reporter: Jon Goldberger [MSFT] <jon.goldberger>
Component: GeneralAssignee: Alexis Christoforides <lexas>
Status: VERIFIED FIXED    
Severity: major CC: adrian.murphy, brendan.zagaeski, felix, kumpera, masafa, michael, mono-bugs+mono, mono-bugs+runtime, shrutis
Priority: High    
Version: 4.0.0   
Target Milestone: mono 4.2 preview 1   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Test Project

Description Jon Goldberger [MSFT] 2015-05-13 14:29:54 UTC
Created attachment 11187 [details]
Test Project

## Description

Calling Dispose on a FileSystemWatcher object causes the below ArgumentOutOfRangeException when a watched directory is deleted, but only if the watched directory has had a file added to it or added and removed from outside of the console app. 

## Steps to reproduce

1. Open the attached test project and set the FSWMono project (a console project) to be the startup project.

2. Edit the path on line 12 to point to a place on your file system where you want the test (and watched) folder to be created. 

3. Run the project. 

4. Add a file to the folder at the path in step 2. 

5. See confirmation in the console that the file was added.

6. press 'd' 

Expected result: Folder will be deleted and "Press any key to exit" will be displayed in the console and app can exit normally.

Actual result: Console app crashes with below exception. 

## Notes

If you remove the added file from the folder before pressing 'd' so the folder is now empty, the app still throws this exception. However if you never add a file to the created folder, this exception does not occur and the app exits normally. 

## Exception

>System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: startIndex
>  at System.String.Substring (Int32 startIndex) [0x0001b] in /private/tmp/source-mono-mac-4.0.0-branch/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.0/mcs/class/corlib/System/String.cs:427
>  at System.IO.KqueueMonitor.PostEvent (FileAction action, System.String path, System.String newPath) [0x00016] in /private/tmp/source-mono-mac-4.0.0-branch/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.0/mcs/class/System/System.IO/KeventWatcher.cs:579
>  at System.IO.KqueueMonitor.Remove (System.IO.PathData pathData) [0x00030] in /private/tmp/source-mono-mac-4.0.0-branch/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.0/mcs/class/System/System.IO/KeventWatcher.cs:464
>  at System.Collections.Generic.List`1[System.IO.PathData].ForEach (System.Action`1 action) [0x00035] in /private/tmp/source-mono-mac-4.0.0-branch/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.0/external/referencesource/mscorlib/system/collections/generic/list.cs:593
>  at System.IO.KqueueMonitor.Monitor () [0x001e8] in /private/tmp/source-mono-mac-4.0.0-branch/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.0/mcs/class/System/System.IO/KeventWatcher.cs:404
>  at System.IO.KqueueMonitor.DoMonitor () [0x00047] in /private/tmp/source-mono-mac-4.0.0-branch/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.0/mcs/class/System/System.IO/KeventWatcher.cs:268


## Environment

=== Xamarin Studio ===

Version 5.9.1 (build 3)
Installation UUID: 2dc9022f-f9a8-424f-8284-bf224cbbfde0
Runtime:
	Mono 4.0.0 ((detached/d136b79)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400000143

=== Apple Developer Tools ===

Xcode 6.3.1 (7703)
Build 6D1002

=== Xamarin.Mac ===

Version: 2.0.0.262 (Business Edition)

=== Xamarin.Android ===

Version: 5.1.0.115 (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: 8.10.0.267 (Business Edition)
Hash: 6481535
Branch: master
Build date: 2015-04-27 04:38:13-0400

=== Build Information ===

Release ID: 509010003
Git revision: aad75a6e7e48f18120ce41f47d0ff2c6216f49c3
Build date: 2015-05-08 12:46:18-04
Xamarin addins: 1246b3044cbb7f56a217334f8fc5489ef8eefe3f

=== Operating System ===

Mac OS X 10.10.3
Darwin Jons-iMac.local 14.3.0 Darwin Kernel Version 14.3.0
    Mon Mar 23 11:59:05 PDT 2015
    root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
Comment 1 Felix Deimel 2015-05-17 08:58:40 UTC
I recently updated to Mono 4.0 and started seeing crash reports coming in that look very similar to this one.

Here's the stack trace of one of those:

System.ArgumentOutOfRangeException: Argument is out of range. 
Parameter name: startIndex 
at System.String.Substring (Int32 startIndex) [0x00000] in <filename unknown>:0 
at System.IO.KqueueMonitor.PostEvent (FileAction action, System.String path, System.String newPath) [0x00000] in <filename unknown>:0 
at System.IO.KqueueMonitor.Monitor () [0x00000] in <filename unknown>:0 
at System.IO.KqueueMonitor.DoMonitor () [0x00000] in <filename unknown>:0 

I looked at the KqueueMonitor source and to me it looks like some additional null checks would fix the issue.
Comment 2 Brendan Zagaeski (Xamarin Support) 2015-05-27 20:23:07 UTC
## Regression status: REGRESSION between Mono 3.12.1 and Mono 4.0.0

BAD:  Mono 4.0.1  (detached/11b5830)
BAD:  Mono 4.0.0  (detached/d136b79)
GOOD: Mono 3.12.1 (detached/0849ec7)




I had some trouble reproducing this problem at first, so here are some adjusted steps that have been working better for me.


## Adjusted steps to reproduce

1. Change the path in "FSWMono/Program.cs" on line 12 to "/tmp/TestFolder".

2. xbuild /t:Build FSWMono.csproj 

3. mono bin/Debug/FSWMono.exe

4. touch /tmp/TestFolder/foo

5. rm /tmp/TestFolder/foo

6. press 'd' 



(In some of my earliest tests it seemed that repeating steps 4 + 5 might have increased the chances of hitting the problem, but in retrospect I believe I actually hit the problem 100% of the time using the above steps even _without_ repeating steps 4 or 5.)




## Supplemental results

- If you switch the path separators, compile on Mono, and then complete the steps to reproduce on Windows .NET, the test project does _not_ hit any exception.

- There is a difference in the "FileSystemEventArgs.File" output in Mono 3.12.1 vs. Mono 4.0.1, but that appears to be a bug _fix_. The behavior in Mono 4.0.1 matches the results on Windows .NET.




## Additional version information

OS X 10.10.3
Comment 4 Rodrigo Kumpera 2015-05-28 11:49:29 UTC
Hey Alexis,

Can you take a look at this one? We'll have to backport it to SR2.
Comment 5 Alexis Christoforides 2015-06-01 23:12:06 UTC
This is now fixed in master: https://github.com/mono/mono/commit/a1828a2aa92eb66bb4d0b8c1fc814a928c5d8fec

The test's Dispose() actually races to sometimes hide the issue. Removing the Dispose call should show the issue every time the directory is deleted.
Comment 6 Rodrigo Kumpera 2015-06-02 14:16:19 UTC
Can we get this one on C5 SR3?
Comment 7 Adrian Murphy 2015-06-03 14:38:10 UTC
@kumpera - works for me, let's put a card on the board to track it
Comment 8 Shruti 2015-07-20 03:03:19 UTC
I have checked this issue with C5SR3 and observed that issue still exists with latest C5SR3 mono MonoFramework-MDK-4.0.3.17.macos10.xamarin.x86_67aef17d51972b5540cdb5d1bd67602bd1c89778.

Can you please let me know with which C5SR3 mono build this get fixed ?
Comment 9 Shruti 2015-07-20 03:06:16 UTC
Update to comment(8) with adding screencast: http://www.screencast.com/t/blCRAvPqX22P
Comment 10 Shruti 2015-07-22 01:56:39 UTC
I have checked this issue with mono  4.0.3.19 and now this issue is working fine.
Screencast regarding same: http://www.screencast.com/t/SyVQMIe4grE

Environment Info:
=== Xamarin Studio ===

Version 5.10 (build 419)
Installation UUID: 67eaf3b4-f8a7-4ab4-a9df-3197350ca5dc
Runtime:
	Mono 4.0.3 ((detached/e4f44c1)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400030019

=== Xamarin.Profiler ===

Version: 0.15.132.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 6.3 (7569)
Build 6D570

=== Xamarin.iOS ===

Version: 8.10.4.27 (Enterprise Edition)
Hash: 7cf6633
Branch: master
Build date: 2015-07-21 16:27:35-0400

=== Xamarin.Android ===

Version: 5.1.5.3 (Enterprise Edition)
Android SDK: /Users/ixamarin78/Desktop/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Xamarin Android Player ===

Version: 0.3.7
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.0.2.102 (Enterprise Edition)

=== Build Information ===

Release ID: 510000419
Git revision: 527e446c9005cb1ab173258a823399afb7770fc8
Build date: 2015-07-19 15:53:40-04
Xamarin addins: dcaf5dc1c5b9c341a9038313f3e6c6615b2e4cff
Build lane: monodevelop-lion-master

=== Operating System ===

Mac OS X 10.10.3
Darwin XamnewiMac.local 14.3.0 Darwin Kernel Version 14.3.0
    Mon Mar 23 11:59:05 PDT 2015
    root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64