diff --git a/MsgPackRpc/MsgPackRpc/MsgPackRpc.swift b/MsgPackRpc/MsgPackRpc/MsgPackRpc.swift index fd4ca156..5c91007e 100644 --- a/MsgPackRpc/MsgPackRpc/MsgPackRpc.swift +++ b/MsgPackRpc/MsgPackRpc/MsgPackRpc.swift @@ -52,8 +52,11 @@ public class Connection { locked(with: condition) { condition.broadcast() } } } - self.stopped = true - self.session.disconnectAndStop() + + locked(with: self.sessionLock) { + self.stopped = true + self.session.disconnectAndStop() + } } @discardableResult @@ -64,6 +67,8 @@ public class Connection { expectsReturnValue: Bool) -> MsgPackRpc.Response { + + let packed = pack( [ .uint(UInt64(type)), @@ -74,18 +79,25 @@ public class Connection { ) guard expectsReturnValue else { - self.session.write(packed) - return self.nilResponse(with: msgid) + return locked(with: self.sessionLock) { + if !self.stopped { + self.session.write(packed) + } + + return self.nilResponse(with: msgid) + } } let condition = NSCondition() locked(with: self.conditionsLock) { self.conditions[msgid] = condition } - if self.stopped { - return self.nilResponse(with: msgid) - } + locked(with: self.sessionLock) { + if self.stopped { + return + } - self.session.write(packed) + self.session.write(packed) + } locked(with: condition) { while !self.stopped && self.responses[msgid] == nil && condition.wait(until: Date(timeIntervalSinceNow: 5)) {} @@ -113,6 +125,7 @@ public class Connection { private var conditions: [UInt32: NSCondition] = [:] private let conditionsLock = NSRecursiveLock() + private let sessionLock = NSRecursiveLock() private var stopped = false private func nilResponse(with msgid: UInt32) -> MsgPackRpc.Response { diff --git a/NvimView/NvimView/NvimView+UiBridge.swift b/NvimView/NvimView/NvimView+UiBridge.swift index a29b63f7..c713d1f6 100644 --- a/NvimView/NvimView/NvimView+UiBridge.swift +++ b/NvimView/NvimView/NvimView+UiBridge.swift @@ -180,8 +180,8 @@ extension NvimView { public func stop() { self.bridgeLogger.hr() - self.agent.quit() self.nvim.disconnect() + self.agent.quit() gui.async { self.waitForNeoVimToQuit()