Bug 25953 - SafeFileHandle
Summary: SafeFileHandle
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-01-12 18:03 UTC by Greg Young
Modified: 2018-03-01 16:04 UTC (History)
4 users (show)

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

testcase (1.67 KB, application/octet-stream)
2015-01-12 18:15 UTC, Zoltan Varga

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:

Description Greg Young 2015-01-12 18:03:16 UTC
Already discussed a bit on mailing list. It seems to happen quite a bit that SafeFileHandle will end up causing a 

_wapi_handle_unref_full: Attempting to unref unused handle 0x6e

From looking I thought I was somehow closing the handle twice but I am not sure how I could be doing that below.

Code included that causes it. Note message happens asynchronously (my guess is on next GC).

        public void shitbird_test() {
           var filename = GetFilePathFor(Guid.NewGuid().ToString());
           using(var stream = new shitstream(filename)) {

    public class shitstream : Stream {
        private SafeFileHandle _handle;

         public shitstream(string filename) {
            var han = Syscall.open(filename, OpenFlags.O_CREAT | OpenFlags.O_RDONLY, FilePermissions.S_IRWXU);
            var _handle = new SafeFileHandle((IntPtr) han, true);
            if(_handle.IsInvalid) throw new Exception("Invalid handle");

        public override void Flush() {}
        public override long Seek(long offset, SeekOrigin origin){return 0;}

        public override void SetLength(long value){}

        public override int Read(byte[] buffer, int offset, int count) {return 0;}
        public override void Write(byte[] buffer, int offset, int count) {}

        public override bool CanRead
            get { return true; }

        public override bool CanSeek
            get  { return true;}

        public override bool CanWrite
            get { return true; }

        public override long Length
            get  { return 0;}

        public override long Position
            get { return 0;}
            set  {}
        protected override void Dispose(bool disposing)
            if(_handle == null) return;
            _handle = null;
            GC.SuppressFinalize (this);   
Comment 1 Zoltan Varga 2015-01-12 18:15:41 UTC
Created attachment 9331 [details]
Comment 2 Greg Young 2015-03-05 16:42:20 UTC
btw there is a small error in code above var_handler should just be _handler.
Comment 3 Ludovic Henry 2018-02-28 23:13:14 UTC
I cannot reproduce with Mono (master/798c5efa52a).
Comment 4 Greg Young 2018-03-01 12:15:15 UTC
I have not tried on 5.13. I will push a request to test again.
Comment 5 Ludovic Henry 2018-03-01 16:04:18 UTC
If you want to try with latest stable (5.10), that would already help quite a bit. You can find it at http://www.mono-project.com/download/stable/. Thank you