Bug 25953 - SafeFileHandle
Summary: SafeFileHandle
Status: NEW
Alias: None
Product: Runtime
Classification: Mono
Component: Interop (show other bugs)
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-01-12 18:03 UTC by Greg Young
Modified: 2015-03-05 16:42 UTC (History)
3 users (show)

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


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

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).

        [Test]
        public void shitbird_test() {
           var filename = GetFilePathFor(Guid.NewGuid().ToString());
           using(var stream = new shitstream(filename)) {
               Console.WriteLine(stream.Position);
           }
           Console.WriteLine("done");
        }

    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.Close();
            _handle = null;
            GC.SuppressFinalize (this);   
        }
    }
Comment 1 Zoltan Varga 2015-01-12 18:15:41 UTC
Created attachment 9331 [details]
testcase
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.

Note You need to log in before you can comment on or make changes to this bug.