From 176dd6e8564d60e936b76f3a896d667ae3acba31 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Fri, 19 Oct 2018 16:52:51 +0100 Subject: [PATCH] fix potential deadlock in BlockingIOThreadPool (#634) Motivation: BlockingIOThreadPool used to call out with a lock held and on top of that on the wrong thread... Modifications: Make BlockingIOThreadPool call out on the supplied `queue` Result: fewer deadlock and surprises --- Sources/NIO/BlockingIOThreadPool.swift | 6 +- .../BlockingIOThreadPoolTest+XCTest.swift | 3 + Tests/NIOTests/BlockingIOThreadPoolTest.swift | 77 +++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Sources/NIO/BlockingIOThreadPool.swift b/Sources/NIO/BlockingIOThreadPool.swift index 2d0ec74b2b..9335186961 100644 --- a/Sources/NIO/BlockingIOThreadPool.swift +++ b/Sources/NIO/BlockingIOThreadPool.swift @@ -68,7 +68,9 @@ public final class BlockingIOThreadPool { self.lock.withLock { switch self.state { case .running(let items): - items.forEach { $0(.cancelled) } + queue.async { + items.forEach { $0(.cancelled) } + } self.state = .shuttingDown(Array(repeating: true, count: numberOfThreads)) (0..