3

I'm getting a crash from GCDWebServer (3.3.3) when my app enters the background:

#3  0x000000010041ea80 in -[GCDWebServer dealloc] at project/Pods/GCDWebServer/GCDWebServer/Core/GCDWebServer.m:221
#4  0x00000001004248b8 in __destroy_helper_block_ ()
#5  0x000000018dd52a28 in _Block_release ()
#6  0x00000001020ad21c in _dispatch_client_callout ()
#7  0x00000001020b2284 in _dispatch_main_queue_callback_4CF ()
#8  0x000000018ee21f2c in __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ ()
#9  0x000000018ee1fb18 in __CFRunLoopRun ()
#10 0x000000018ed4e048 in CFRunLoopRunSpecific ()
#11 0x00000001907d1198 in GSEventRunModal ()
#12 0x0000000194d28628 in -[UIApplication _run] ()
#13 0x0000000194d23360 in UIApplicationMain ()
#14 0x000000010009243c in main at project/main.m:10
#15 0x000000018dd305b8 in start ()
Enqueued from com.apple.main-thread (Thread 1)Queue : com.apple.main-thread (serial)
#0  0x00000001020b8ba4 in _dispatch_queue_push ()
#1  0x0000000100424680 in -[GCDWebServer _stop] at project/Pods/GCDWebServer/GCDWebServer/Core/GCDWebServer.m:734
#2  0x0000000100424a10 in -[GCDWebServer _didEnterBackground:] at project/Pods/GCDWebServer/GCDWebServer/Core/GCDWebServer.m:746

The specific line is:

GWS_DCHECK(_options == nil);  // The server can never be dealloc'ed while running because of the retain-cycle with the dispatch source

It looks like the _options dictionary has to be nil (eg. the server has to be stopped) but it looks like the _options is never set to nil on this code path: it's set to nil in - stop but not in - _stop.

I'm probably missing something as this would have been noticed by other people.

Kamchatka
  • 3,597
  • 4
  • 38
  • 69

1 Answers1

7

I had the same problem. I solved it saving the server in a static variable on my class instead of saving in a function.

It doesn't works:

    class Server {
        static func initialize() {
            let webServer = GCDWebServer()
            ...
            webServer?.start(withPort: 8081, bonjourName: nil)
        }
    }

It's works:

    class Server {
        static let webServer = GCDWebServer()
        static func initialize() {
            ...
            webServer?.start(withPort: 8081, bonjourName: nil)
        }
    }
  • 1
    Not sure why the asker did not mark this as the answer, but I faced the same problem and I can verify that this works for me. I am using objc, so what I did was create a strong reference to webServer, and that solves the problem. – wahkiz Jun 07 '17 at 07:51
  • I was having the same issue with a package using GCD, worked the same way, thanks ! – Off Jun 03 '20 at 08:53