Skip to content

Commit

Permalink
Properly close DispatchStreams
Browse files Browse the repository at this point in the history
- We think that not closing these explicitely is causing the main descriptor to
be in a "random" state. Other applications may fail on lock or just skip a read
because the descriptor may be set as non-blocking, etc...
- Tested opening ssh and then opening mosh. The mosh client would randomly
terminate. We think this was also causing random ios_exit issues and Blink's
prompt crashes.
  • Loading branch information
Carlos Cabanero committed Apr 9, 2024
1 parent 1c558ce commit 6cae73e
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions SSH/DispatchStreams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public enum DispatchStreamError: Error {
public class DispatchOutputStream: Writer {
let stream: DispatchIO
let queue: DispatchQueue
var fd: Int32?

public init(stream: Int32) {
self.fd = stream
self.queue = DispatchQueue(label: "file-\(stream)")
self.stream = DispatchIO(type: .stream, fileDescriptor: stream, queue: self.queue, cleanupHandler: { error in
print("Dispatch closed with \(error)")
Expand Down Expand Up @@ -82,15 +84,26 @@ public class DispatchOutputStream: Writer {
}

public func close() {
stream.close(flags: .stop)
if self.fd != nil {
stream.close(flags: .stop)
self.fd = nil
}
}

deinit {
if self.fd != nil {
stream.close(flags: .stop)
}
}
}

public class DispatchInputStream {
let stream: DispatchIO
let queue: DispatchQueue
var fd: Int32?

public init(stream: Int32) {
self.fd = stream
self.queue = DispatchQueue(label: "file-\(stream)")
self.stream = DispatchIO(type: .stream, fileDescriptor: stream, queue: self.queue, cleanupHandler: { error in
print("Dispatch \(error)")
Expand All @@ -99,7 +112,16 @@ public class DispatchInputStream {
}

public func close() {
stream.close(flags: .stop)
if self.fd != nil {
stream.close(flags: .stop)
self.fd = nil
}
}

deinit {
if self.fd != nil {
stream.close(flags: .stop)
}
}
}

Expand Down

0 comments on commit 6cae73e

Please sign in to comment.