Bug 27086 - Writing in asynchronous FileStream adds 0 bytes at the beginning of the file
Summary: Writing in asynchronous FileStream adds 0 bytes at the beginning of the file
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-02-16 03:35 UTC by guillaume.turri
Modified: 2015-02-18 07:34 UTC (History)
2 users (show)

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


Attachments
Test case (1.05 KB, text/x-csharp)
2015-02-16 03:35 UTC, guillaume.turri
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 guillaume.turri 2015-02-16 03:35:31 UTC
Created attachment 9843 [details]
Test case

Hi,

I just tested this on a version of Mono build from source, from the current HEAD (bc57fda921d7c35dd7f52f71bdbdff9e831f328e) on Ubuntu.

* How to reproduce: Run the code attached with this bug report (and copy/pasted below)
* Expected behavior: We can read the DateTime we just wrote (ie: 03/04/1985)
* Behavior with .Net: same as the expected one
* Actual behavior: The DateTime read is "01/01/0001 12:00 AM"

Some more details:
* When we remove the FileOptions.Asynchronous flag, it works fine
* If we don't write the Guid (ie: if we only write the TimeStamp), it works fine
* When we open the file with an hexadecimal editor, it seems the file starts with several 0 bytes, and then we have the expected content (and "01/01/0001 12:00 AM" is the result of DateTime.FromBinary(0), so the issue if likely when we write, not when we read).

Regards,
Guillaume


---

    using System;
    using System.IO;
    using System.Linq;
    
    public class Program
    {
    	public static void Main(String[] args)
    	{
    		string filename = "myfilename";
    		File.Delete (filename);
    
    		using (var file = new FileStream (filename, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.Asynchronous))
    		using (var writer = new BinaryWriter (file)){
    			WriteTimeStamp (writer);
    			WriteGuid (writer);
     
    			writer.Flush ();
     
    			file.Close ();
    			writer.Close ();
    		}
    		ReadFile (filename);
    	}
    
    	private static void WriteTimeStamp(BinaryWriter writer){
    		var timeStamp = new DateTime (1985, 04, 03);
    		writer.Write (timeStamp.ToBinary ());
    	}
    
    	private static void WriteGuid(BinaryWriter writer){
    		var id = new Guid (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    		writer.Write (id.ToByteArray ());
    	}
    
    	private static void ReadFile(string filename){
    		using (var inputStream = new FileStream (filename, FileMode.Open))
    		using (var reader = new BinaryReader (inputStream)) {
    			var timeStamp = DateTime.FromBinary (reader.ReadInt64 ());
    			Console.WriteLine (timeStamp);
    		}
    	}
    }
Comment 1 Marek Safar 2015-02-18 06:57:02 UTC
Fixed in master
Comment 2 guillaume.turri 2015-02-18 07:34:56 UTC
Perfect!
Thanks!