             print("[BRHTTPServer] already suspended")
         }
     }
-    
+
     func resume(_: Notification) {
+        objc_sync_enter(self)
+        defer { objc_sync_exit(self) }
+        isShutdownCancelled = true
         if !isStarted {
             do {
                 try listenServer(port)

    
    func suspend(_: Notification) {
        if isStarted {
            if self.clients.count == 0 {
                shutdownServer()
                print("[BRHTTPServer] suspended")
            } else {
                // give it 500ms to complete, then kill it
                print("[BRHTTPServer] suspending: waiting for clients")
                Q.asyncAfter(deadline: .now() + .milliseconds(500)) {
                    self.shutdownServer()
                    print("[BRHTTPServer] suspended")
                }
            }

    var isStarted: Bool { return fd != -1 }
    var port: in_port_t = 0
    var listenAddress: String = "127.0.0.1"
    private var isShutdownCancelled = false
    var Q: DispatchQueue = {
        return DispatchQueue(label: "br_http_server", attributes: DispatchQueue.Attributes.concurrent)
    }()
    
    func prependMiddleware(middleware mw: BRHTTPMiddleware) {
        middleware.insert(mw, at: 0)
