Bug 12777 - FileStream.BeginWrite maxing out CPU with no progress on second call
Summary: FileStream.BeginWrite maxing out CPU with no progress on second call
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-06-19 19:07 UTC by Eugen Rieck
Modified: 2013-08-14 11:52 UTC (History)
6 users (show)

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


Attachments
Test case, zipped MonoDevelop solution folder (8.75 KB, application/x-zip-compressed)
2013-06-19 19:07 UTC, Eugen Rieck
Details
Collection of more test cases, zip MonoDevelop solution folder (15.09 KB, application/x-zip-compressed)
2013-06-19 20:05 UTC, Eugen Rieck
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 Eugen Rieck 2013-06-19 19:07:04 UTC
Created attachment 4162 [details]
Test case, zipped MonoDevelop solution folder

Steps to reproduce:
- Open a FileStream to a new file for writing
- Call BeginWrite() with a callback
- The callback is invoked as expected
- In the callback, call EndWrite()
- After the successfull EndWrite, invoke another BeginWrite() with the same callback
- The callback is never invoked a second time
- Mono maxes out a single CPU core
- Nothing is written to the file stream (not even the first buffer)

Testcase attached, verified with buffer size 1048576 (1MiB) and stream length 104857600 (100MiB)

Hardware:
HP XW9400, AMD Opteron 2220 (2 Sockets, 2 Cores each), 12GB RAM

OS version:
Ubuntu Linux (12.04 AMD64, fully patched): 
Linux PreciseHome 3.2.0-48-generic #74-Ubuntu SMP Thu Jun 6 19:43:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux)

Mono versions checked:
- latest badgerports version
  Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-5~dhx1~precise1)
  Copyright (C) 2002-2011 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:            Included Boehm (with typed GC and Parallel Mark)

- 3.0.7 compiled on target system
  Mono JIT compiler version 3.0.7 (tarball Mit Mär 20 16:27:48 CET 2013)
  Copyright (C) 2002-2012 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:            Included Boehm (with typed GC and Parallel Mark)
Comment 1 Eugen Rieck 2013-06-19 20:05:11 UTC
Created attachment 4163 [details]
Collection of more test cases, zip MonoDevelop solution folder

More research shows, that this seems to come from the BufferedStream side of things: 

- The original test class is new testcase variant "S"

- Calling the next BeginWrite() on a thread pool thread to avoid calling BeginWrite() from inside the completion callback changes nothing (See testcase variant "T")

- Wrapping the FileStream inside a BufferedStream, then acting on this makes the problem worse (Second BeginWrite() never returns, see testcase variant "B")

- Calling Flush() immediately after EndWrite() to sidestep the buffering cures the problem (and ofcourse kills the performance) (See testcase variant "F")
Comment 2 Marek Safar 2013-07-05 12:47:58 UTC
Fixed in master