[Swift NIO - iOS] Crash app after restarting Websocket server

I used websocket server following this example "swift-nio/main.swift at main · apple/swift-nio · GitHub".

I created singleton websocket with 2 function

  1. Start server
func start() {
        if self.host == "" {
            print("debug: websocket cannot start because no internet connection!")
        }
        
        let bootstrap = ServerBootstrap(group: group)
            // Specify backlog and enable SO_REUSEADDR for the server itself
            .serverChannelOption(ChannelOptions.backlog, value: 256)
            .serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)

            // Set the handlers that are applied to the accepted Channels
            .childChannelInitializer { channel in
                let httpHandler = HTTPHandler()
                let config: NIOHTTPServerUpgradeConfiguration = (
                    upgraders: [ self.upgrader ],
                                completionHandler: { _ in
                                    channel.pipeline.removeHandler(httpHandler, promise: nil)
                                }
                            )
                return channel.pipeline.configureHTTPServerPipeline(withServerUpgrade: config).flatMap {
                    channel.pipeline.addHandler(httpHandler)
                }
            }

            // Enable SO_REUSEADDR for the accepted Channels
            .childChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)

        let channel = try? bootstrap.bind(host: host, port: port).wait()

        guard let localAddress = channel?.localAddress else {
            fatalError("Address was unable to bind. Please check that the socket was not closed or that the address family was understood.")
        }
        print("Server started and listening on \(localAddress)")
        isRunning = true
        streamUrl = "http://\(self.host):\(self.port)"

        // This will never unblock as we don't close the ServerChannel
        try? channel?.closeFuture.wait()

        print("Server closed")
    }
  1. Stop server
func stop() {
        if self.host == "" {
            print("debug: websocket cannot start because no internet connection!")
            return
        }
        try? group.syncShutdownGracefully()
        isRunning = false
        print("debug: websocket is stopped!")
    }

I try start server with start func -> Server worked great.
After that, i stop server with stop func -> Server is stopped.
But when i start server again -> app is crashed.

Below is error screen shot

Anybody can help me?

Looks like your channel is still open when you shutdown? Try closing the channel before shutting down the EventLoopGroup.