Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-13918] NWListener can't cancel and relisten #56316

Open
swift-ci opened this issue Nov 29, 2020 · 1 comment
Open

[SR-13918] NWListener can't cancel and relisten #56316

swift-ci opened this issue Nov 29, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13918
Radar None
Original Reporter jhihn (JIRA User)
Type Bug
Environment

iOS13

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: ead23e7ba24e3180f0aa4a0f99b52063

Issue Description:

I have an app on iOS. I am to listen only while the app is in the foreground, and re-listen when it come back from background. However I call my stop() function and if I call start() subsequently it fails. Also if the app is terminated (in debugger or task manager) I cannot launch the app again for ~5 seconds because the listen() will fail.

class SwiftWebSocketServer {
 let port: NWEndpoint.Port
 var listener: NWListener?
 var listenerState: NWListener.State
 let eventHandler:()->Void
 var connection: ServerConnection?
init(port: UInt16, handler:@escaping ()->Void) {
 self.port = NWEndpoint.Port(rawValue: port)!
 listenerState = .cancelled
 self.eventHandler = handler
 let parameters = NWParameters(tls: nil)
 parameters.allowLocalEndpointReuse = true
 parameters.includePeerToPeer = true
 let wsOptions = NWProtocolWebSocket.Options()
 wsOptions.autoReplyPing = true
 parameters.defaultProtocolStack.applicationProtocols.insert(wsOptions, at: 0)
 do {
 listener = try NWListener(using: parameters, on: self.port)
 listener!.stateUpdateHandler = self.stateDidChange(to:)
 listener!.newConnectionHandler = self.didAccept(nwConnection:)
 } catch {
 print(#function, error)
 }
 }
func start() throws {
 print("Server starting...")
 listener!.stateUpdateHandler = self.stateDidChange(to:)
 listener!.newConnectionHandler = self.didAccept(nwConnection:)
 listener!.start(queue: .main)
 print("Server started.")
 eventHandler()
 }
func stop() {
 self.listener!.stateUpdateHandler = nil
 self.listener!.newConnectionHandler = nil
 self.listener!.cancel()
 print("Server cancelled")
 connection?.stop()
 connection?.didStopCallback = nil
 connection = nil
 eventHandler()
 }
 func stateDidChange(to newState: NWListener.State) {
 print(#function, newState)
 switch newState {
 case .ready:
 print("Server ready.")
 case .failed(let error):
 print("Server failure, error: \(error.localizedDescription)")
 exit(EXIT_FAILURE)
 default:
 break
 }
 listenerState = newState
 eventHandler()
 }
}
Server starting...
2020-08-12 16:29:42.476799-0400 rfa-ios-native[821:464825]  nw listener set queue Error in client: nw listener set queue called after nw listener start
2020-08-12 16:29:42.480218-0400 rfa-ios-native[821:464825]  nw listener set queue Error in client: nw listener set queue called after nw listener start, dumping backtrace:
[arm64] libnetcore-1880.100.30
0 libnetwork.dylib 0x000000019218b060 _ nw create backtrace string + 120
1 libnetwork.dylib 0x00000001920a68bc nw listener set queue + 228
2 libswiftNetwork.dylib 0x00000001c6556e74 $s7Network10NWListenerC5start5queueySo012OS dispatch D0C tF + 56
...
Server started.
2020-08-12 16:29:42.482909-0400 rfa-ios-native[821:464835]  nw listener start block invoke [L1] In wrong state for start
2020-08-12 16:29:42.483533-0400 rfa-ios-native[821:464835]  nw listener start block invoke [L1] In wrong state for start, dumping backtrace:
[arm64] libnetcore-1880.100.30
0 libnetwork.dylib 0x000000019218b060 _ nw create backtrace string + 120
1 libnetwork.dylib 0x00000001920a89e8 6B86071A-0DFC-3EFD-A798-615EC6A7D591 + 3578344
2 libdispatch.dylib 0x000000010068dd10 dispatch call block and_release + 32
 

Links:

https://stackoverflow.com/questions/63178889/swift-nwlistener-listen-cancel-and-relisten-successfully/64999960#64999960

https://forums.swift.org/t/how-to-listen-cancel-and-re-listen-with-nwlistener/39354/2

@typesanitizer
Copy link

Hi! Thank you for the bug report. Looks like this issue is based on a specific Apple framework, and not an issue related to a specific part of the open source toolchain (compiler, stdlib, lldb, SourceKit etc.). Bug reports for Apple frameworks can be reported through feedbackassistant.apple.com.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants