                         } else {
                             // Must be an unrecoverable error, close the channel.
                             // TODO: Maybe don't close if you want half-open channel
-                            dispatch_io_close(self.channel, 0)
+                            // NOTE: This will be done by onCompleted or onError
+                            // dispatch_io_close(self.channel, 0)
                         }
                     }
                 }
             }
             return AnonymousDisposable { [fd = self.fd] in
                 log.verbose("Disposing \(fd) for writing.")
-                dispatch_io_close(self.channel, 0)
+                dispatch_io_close(writeChannel, 0)
             }
         }
     }

                    
                    // It's done close the channel
                    // TODO: Maybe don't close if you want half-open channel
                    dispatch_io_close(self.channel, 0)
                }
            }
            return AnonymousDisposable { [fd = self.fd] in
                log.verbose("Disposing \(fd) for reading.")
                dispatch_io_close(self.channel, 0)
            }
        }
    }

    
    func write(buffer: [UInt8]) -> Observable<[UInt8]> {
        return Observable.create { (observer) -> Disposable in
            
            let writeChannel = dispatch_io_create_with_io(
                DISPATCH_IO_STREAM,
                self.channel,
                dispatch_get_main_queue()
            ) { error in
                observer.onError(error: Error(rawValue: error))
            }!
            
            buffer.withUnsafeBufferPointer { buffer in
                
                // Allocate dispatch data
