Skip to content
New issue

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

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

Already on GitHub? # to your account

Crash when using very tight or past deadline #71

Closed
ldewailly opened this issue Jul 17, 2019 · 3 comments · Fixed by apple/swift-nio#1074
Closed

Crash when using very tight or past deadline #71

ldewailly opened this issue Jul 17, 2019 · 3 comments · Fixed by apple/swift-nio#1074

Comments

@ldewailly
Copy link

The following code causes a crash:

_ = try httpClient.get(url: "https://github.com", deadline: .now() + .milliseconds(1)).wait()

The backtrace is as follows:

* thread #4, name = 'NIO-ELT-#0', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
  * frame #0: 0x00000001070cabe1 AsyncHTTPClientTests`static NIODeadline.- infix(lhs=(uptimeNanoseconds = 247397817094645), rhs=(uptimeNanoseconds = 247397825576649), self=NIO.NIODeadline) at EventLoop.swift:397:68
    frame #1: 0x00000001073d47a3 AsyncHTTPClientTests`HTTPClient.resolve(timeout=nil, deadline=(uptimeNanoseconds = 247397817094645), self=0x0000000100e05270) at HTTPClient.swift:175:29
    frame #2: 0x00000001073d3dff AsyncHTTPClientTests`closure #2 in closure #2 in HTTPClient.execute<A>(self=0x0000000100e05270, deadline=(uptimeNanoseconds = 247397817094645), channel=(instance = 0x0000000100c1f450 -> 0x00000001074c0718 type metadata for NIO.SocketChannel, witness_table_Channel = 0x0000000107499d18 AsyncHTTPClientTests`protocol witness table for NIO.BaseSocketChannel<A> : NIO.Channel in NIO)) at HTTPClient.swift:138:43
    frame #3: 0x00000001073de2ae AsyncHTTPClientTests`partial apply for closure #2 in closure #2 in HTTPClient.execute<A>(request:delegate:deadline:) at <compiler-generated>:0
    frame #4: 0x00000001073d3d1c AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed () -> (@owned EventLoopFuture<()>) at <compiler-generated>:0
    frame #5: 0x00000001073de2f1 AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed () -> (@owned EventLoopFuture<()>)partial apply at <compiler-generated>:0
    frame #6: 0x00000001070df330 AsyncHTTPClientTests`closure #1 in EventLoopFuture.flatMap<Value>(self=0x0000000100c23bf0, callback=0x00000001073de2e0 AsyncHTTPClientTests`reabstraction thunk helper from @escaping @callee_guaranteed () -> (@owned NIO.EventLoopFuture<()>) to @escaping @callee_guaranteed (@in_guaranteed ()) -> (@owned NIO.EventLoopFuture<()>)partial apply forwarder with unmangled suffix ".28" at <compiler-generated>, next=NIO.EventLoopPromise<()> @ 0x0000700007b469f8) at EventLoopFuture.swift:468:31
    frame #7: 0x00000001070e4c68 AsyncHTTPClientTests`partial apply for closure #1 in EventLoopFuture.flatMap<A>(file:line:_:) at <compiler-generated>:0
    frame #8: 0x00000001070dfbe8 AsyncHTTPClientTests`EventLoopFuture._addCallback(callback=0x00000001070e4c40 AsyncHTTPClientTests`partial apply forwarder for closure #1 () -> NIO.CallbackList in NIO.EventLoopFuture.flatMap<A>(file: Swift.StaticString, line: Swift.UInt, _: (A) -> NIO.EventLoopFuture<A1>) -> NIO.EventLoopFuture<A1> at <compiler-generated>, self=0x0000000100c23bf0) at EventLoopFuture.swift:634:16
    frame #9: 0x00000001070dfd42 AsyncHTTPClientTests`EventLoopFuture._whenComplete(callback=0x00000001070e4c40 AsyncHTTPClientTests`partial apply forwarder for closure #1 () -> NIO.CallbackList in NIO.EventLoopFuture.flatMap<A>(file: Swift.StaticString, line: Swift.UInt, _: (A) -> NIO.EventLoopFuture<A1>) -> NIO.EventLoopFuture<A1> at <compiler-generated>, self=0x0000000100c23bf0) at EventLoopFuture.swift:641:18
    frame #10: 0x00000001070def9d AsyncHTTPClientTests`EventLoopFuture.flatMap<Value>(file="/Users/ldewailly/devex/async-http-client/Sources/AsyncHTTPClient/HTTPClient.swift", line=137, callback=0x00000001073de2e0 AsyncHTTPClientTests`reabstraction thunk helper from @escaping @callee_guaranteed () -> (@owned NIO.EventLoopFuture<()>) to @escaping @callee_guaranteed (@in_guaranteed ()) -> (@owned NIO.EventLoopFuture<()>)partial apply forwarder with unmangled suffix ".28" at <compiler-generated>, self=0x0000000100c23bf0) at EventLoopFuture.swift:465:14
    frame #11: 0x00000001073d2820 AsyncHTTPClientTests`closure #2 in HTTPClient.execute<T>(channel=(instance = 0x0000000100c1f450 -> 0x00000001074c0718 type metadata for NIO.SocketChannel, witness_table_Channel = 0x0000000107499d18 AsyncHTTPClientTests`protocol witness table for NIO.BaseSocketChannel<A> : NIO.Channel in NIO), self=0x0000000100e05270, request=AsyncHTTPClient.HTTPClient.Request @ 0x0000000100b93cc0, deadline=(uptimeNanoseconds = 247397817094645), task=0x0000000100b92dd0, delegate=0x0000700007b46fa0, redirectHandler=nil) at HTTPClient.swift:137:19
    frame #12: 0x00000001073d8f9a AsyncHTTPClientTests`partial apply for closure #2 in HTTPClient.execute<A>(request:delegate:deadline:) at <compiler-generated>:0
    frame #13: 0x0000000107058b00 AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed (@guaranteed Channel) -> (@owned EventLoopFuture<()>) at <compiler-generated>:0
    frame #14: 0x0000000107062a01 AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed (@guaranteed Channel) -> (@owned EventLoopFuture<()>)partial apply at <compiler-generated>:0
    frame #15: 0x0000000107058bf8 AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed (@in_guaranteed Channel) -> (@out EventLoopFuture<()>) at <compiler-generated>:0
    frame #16: 0x00000001070627b1 AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed (@in_guaranteed Channel) -> (@out EventLoopFuture<()>)partial apply at <compiler-generated>:0
    frame #17: 0x000000010705f102 AsyncHTTPClientTests`setupChannel #1 (eventLoop=(instance = 0x0000000100b91430 -> 0x00000001074bf628 type metadata for NIO.SelectableEventLoop, witness_table_EventLoop = 0x000000010749d9e0 AsyncHTTPClientTests`protocol witness table for NIO.SelectableEventLoop : NIO.EventLoop in NIO), channelInitializer=0x00000001070627a0 AsyncHTTPClientTests`reabstraction thunk helper from @escaping @callee_guaranteed (@in_guaranteed NIO.Channel) -> (@out NIO.EventLoopFuture<()>) to @escaping @callee_guaranteed (@guaranteed NIO.Channel) -> (@owned NIO.EventLoopFuture<()>)partial apply forwarder with unmangled suffix ".126" at <compiler-generated>, channel=0x0000000100c1f450, channelOptions=NIO.ChannelOptions.Storage @ 0x0000700007b472b8, body=0x000000010705c6f0 AsyncHTTPClientTests`closure #1 (NIO.Channel) -> NIO.EventLoopFuture<()> in closure #1 (NIO.EventLoop, Swift.Int32) -> NIO.EventLoopFuture<NIO.Channel> in NIO.ClientBootstrap.connect(host: Swift.String, port: Swift.Int) -> NIO.EventLoopFuture<NIO.Channel> at Bootstrap.swift:433, promise=NIO.EventLoopPromise<Channel> @ 0x0000700007b472a0) in ClientBootstrap.execute(eventLoop:protocolFamily:_:) at Bootstrap.swift:520:13
    frame #18: 0x000000010705ce2e AsyncHTTPClientTests`ClientBootstrap.execute(eventLoop=(instance = 0x0000000100b91430 -> 0x00000001074bf628 type metadata for NIO.SelectableEventLoop, witness_table_EventLoop = 0x000000010749d9e0 AsyncHTTPClientTests`protocol witness table for NIO.SelectableEventLoop : NIO.EventLoop in NIO), protocolFamily=2, body=0x000000010705c6f0 AsyncHTTPClientTests`closure #1 (NIO.Channel) -> NIO.EventLoopFuture<()> in closure #1 (NIO.EventLoop, Swift.Int32) -> NIO.EventLoopFuture<NIO.Channel> in NIO.ClientBootstrap.connect(host: Swift.String, port: Swift.Int) -> NIO.EventLoopFuture<NIO.Channel> at Bootstrap.swift:433, self=0x0000000100b934f0) at Bootstrap.swift:534:20
    frame #19: 0x000000010705c699 AsyncHTTPClientTests`closure #1 in ClientBootstrap.connect(eventLoop=(instance = 0x0000000100b91430 -> 0x00000001074bf628 type metadata for NIO.SelectableEventLoop, witness_table_EventLoop = 0x000000010749d9e0 AsyncHTTPClientTests`protocol witness table for NIO.SelectableEventLoop : NIO.EventLoop in NIO), protocolFamily=2, self=0x0000000100b934f0) at Bootstrap.swift:433:25
    frame #20: 0x000000010705c6bc AsyncHTTPClientTests`partial apply for closure #1 in ClientBootstrap.connect(host:port:) at <compiler-generated>:0
    frame #21: 0x00000001070f87ed AsyncHTTPClientTests`HappyEyeballsConnector.connectToTarget(target=v4, self=0x0000000100b94370) at HappyEyeballs.swift:529:29
    frame #22: 0x00000001070f7f6d AsyncHTTPClientTests`HappyEyeballsConnector.beginConnecting(self=0x0000000100b94370) at HappyEyeballs.swift:459:9
    frame #23: 0x00000001070f815e AsyncHTTPClientTests`HappyEyeballsConnector.connectToNewTargets(self=0x0000000100b94370) at HappyEyeballs.swift:474:9
    frame #24: 0x00000001070f73f1 AsyncHTTPClientTests`HappyEyeballsConnector.processInput(input=resolverACompleted, self=0x0000000100b94370) at HappyEyeballs.swift:374:13
    frame #25: 0x00000001070fa54a AsyncHTTPClientTests`closure #3 in HappyEyeballsConnector.whenALookupComplete(self=0x0000000100b94370) at HappyEyeballs.swift:611:18
    frame #26: 0x00000001070fc945 AsyncHTTPClientTests`partial apply for closure #3 in HappyEyeballsConnector.whenALookupComplete(future:) at <compiler-generated>:0
    frame #27: 0x000000010705da90 AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed (@guaranteed Result<(), Error>) -> () at <compiler-generated>:0
    frame #28: 0x00000001070fc991 AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed (@guaranteed Result<(), Error>) -> ()partial apply at <compiler-generated>:0
    frame #29: 0x00000001070e27fa AsyncHTTPClientTests`closure #1 in EventLoopFuture.whenComplete(callback=0x00000001070fc980 AsyncHTTPClientTests`reabstraction thunk helper from @escaping @callee_guaranteed (@guaranteed Swift.Result<(), Swift.Error>) -> () to @escaping @callee_guaranteed (@in_guaranteed Swift.Result<(), Swift.Error>) -> ()partial apply forwarder with unmangled suffix ".18" at <compiler-generated>, self=0x0000000100c1ec40) at EventLoopFuture.swift:709:13
    frame #30: 0x00000001070e5175 AsyncHTTPClientTests`partial apply for closure #1 in EventLoopFuture.whenComplete(_:) at <compiler-generated>:0
    frame #31: 0x00000001070dfbe8 AsyncHTTPClientTests`EventLoopFuture._addCallback(callback=0x00000001070e5160 AsyncHTTPClientTests`partial apply forwarder for closure #1 () -> NIO.CallbackList in NIO.EventLoopFuture.whenComplete((Swift.Result<A, Swift.Error>) -> ()) -> () at <compiler-generated>, self=0x0000000100c1ec40) at EventLoopFuture.swift:634:16
    frame #32: 0x00000001070dfd42 AsyncHTTPClientTests`EventLoopFuture._whenComplete(callback=0x00000001070e5160 AsyncHTTPClientTests`partial apply forwarder for closure #1 () -> NIO.CallbackList in NIO.EventLoopFuture.whenComplete((Swift.Result<A, Swift.Error>) -> ()) -> () at <compiler-generated>, self=0x0000000100c1ec40) at EventLoopFuture.swift:641:18
    frame #33: 0x00000001070e25eb AsyncHTTPClientTests`EventLoopFuture.whenComplete(callback=0x00000001070fc980 AsyncHTTPClientTests`reabstraction thunk helper from @escaping @callee_guaranteed (@guaranteed Swift.Result<(), Swift.Error>) -> () to @escaping @callee_guaranteed (@in_guaranteed Swift.Result<(), Swift.Error>) -> ()partial apply forwarder with unmangled suffix ".18" at <compiler-generated>, self=0x0000000100c1ec40) at EventLoopFuture.swift:708:14
    frame #34: 0x00000001070fa35f AsyncHTTPClientTests`HappyEyeballsConnector.whenALookupComplete(future=0x0000000100b941c0, self=0x0000000100b94370) at HappyEyeballs.swift:609:11
    frame #35: 0x00000001070f79dc AsyncHTTPClientTests`HappyEyeballsConnector.beginDNSResolution(self=0x0000000100b94370) at HappyEyeballs.swift:432:9
    frame #36: 0x00000001070f6fa0 AsyncHTTPClientTests`HappyEyeballsConnector.processInput(input=resolve, self=0x0000000100b94370) at HappyEyeballs.swift:322:13
    frame #37: 0x00000001070f6e2c AsyncHTTPClientTests`closure #1 in HappyEyeballsConnector.resolveAndConnect(self=0x0000000100b94370) at HappyEyeballs.swift:308:18
    frame #38: 0x00000001070f6e4c AsyncHTTPClientTests`partial apply for closure #1 in HappyEyeballsConnector.resolveAndConnect() at <compiler-generated>:0
    frame #39: 0x00000001070d0bec AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed () -> () at <compiler-generated>:0
    frame #40: 0x00000001070d9541 AsyncHTTPClientTests`partial apply for thunk for @escaping @callee_guaranteed () -> () at <compiler-generated>:0
    frame #41: 0x00000001070d0c0c AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed () -> (@out ()) at <compiler-generated>:0
    frame #42: 0x00000001070d67f1 AsyncHTTPClientTests`partial apply for thunk for @escaping @callee_guaranteed () -> (@out ()) at <compiler-generated>:0
    frame #43: 0x00000001070d0c57 AsyncHTTPClientTests`closure #3 in SelectableEventLoop.run(task=0x00000001070d67e0 AsyncHTTPClientTests`partial apply forwarder for reabstraction thunk helper from @escaping @callee_guaranteed () -> (@out ()) to @escaping @callee_guaranteed () -> () at <compiler-generated>) at EventLoop.swift:873:25
    frame #44: 0x00000001070d6771 AsyncHTTPClientTests`partial apply for closure #3 in SelectableEventLoop.run() at <compiler-generated>:0
    frame #45: 0x00000001070484c6 AsyncHTTPClientTests`thunk for @callee_guaranteed () -> (@error @owned Error) at <compiler-generated>:0
    frame #46: 0x00000001070d679b AsyncHTTPClientTests`thunk for @callee_guaranteed () -> (@error @owned Error)partial apply at <compiler-generated>:0
    frame #47: 0x00000001070cc4a9 AsyncHTTPClientTests`closure #1 in withAutoReleasePool<T>(execute=0x00000001070d6780 AsyncHTTPClientTests`reabstraction thunk helper from @callee_guaranteed () -> (@error @owned Swift.Error) to @escaping @callee_guaranteed () -> (@out (), @error @owned Swift.Error)partial apply forwarder with unmangled suffix ".23" at <compiler-generated>) at EventLoop.swift:587:13
    frame #48: 0x00000001070d9576 AsyncHTTPClientTests`partial apply for closure #1 in withAutoReleasePool<A>(_:) at <compiler-generated>:0
    frame #49: 0x00007fff6e8acf0e libswiftObjectiveC.dylib`ObjectiveC.autoreleasepool<A>(invoking: () throws -> A) throws -> A + 46
    frame #50: 0x00000001070cc440 AsyncHTTPClientTests`withAutoReleasePool<T>(execute=0x00000001070d6780 AsyncHTTPClientTests`reabstraction thunk helper from @callee_guaranteed () -> (@error @owned Swift.Error) to @escaping @callee_guaranteed () -> (@out (), @error @owned Swift.Error)partial apply forwarder with unmangled suffix ".23" at <compiler-generated>) at EventLoop.swift:586:16
    frame #51: 0x00000001070cfbd5 AsyncHTTPClientTests`SelectableEventLoop.run(self=0x0000000100b91430) at EventLoop.swift:872:21
    frame #52: 0x00000001070d327b AsyncHTTPClientTests`closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(t=(pthread = 0x0000700007b49000), initializer=0x00000001070d9000 AsyncHTTPClientTests`partial apply forwarder for reabstraction thunk helper from @escaping @callee_guaranteed (@in_guaranteed NIO.NIOThread) -> (@out ()) to @escaping @callee_guaranteed (@guaranteed NIO.NIOThread) -> () at <compiler-generated>, self=0x00000001074bf738, lock=(mutex = 0x0000000100e01950), _loop=0x0000000100b91430, loopUpAndRunningGroup=0x0000000100e03070) at EventLoop.swift:1039:23
    frame #53: 0x00000001070d90da AsyncHTTPClientTests`partial apply for closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:initializer:) at <compiler-generated>:0
    frame #54: 0x00000001070d382f AsyncHTTPClientTests`thunk for @escaping @callee_guaranteed (@guaranteed NIOThread) -> () at <compiler-generated>:0
    frame #55: 0x000000010717c381 AsyncHTTPClientTests`partial apply for thunk for @escaping @callee_guaranteed (@guaranteed NIOThread) -> () at <compiler-generated>:0
    frame #56: 0x000000010717c73b AsyncHTTPClientTests`closure #1 in static NIOThread.spawnAndRun(p=(_rawValue = 0x0000000100e0aba0 -> 0x00007fff9b424cf0 libswiftCore.dylib`InitialAllocationPool + 2808)) at Thread.swift:104:13
    frame #57: 0x000000010717c7b9 AsyncHTTPClientTests`@objc closure #1 in static NIOThread.spawnAndRun(name:body:) at <compiler-generated>:0
    frame #58: 0x00007fff6edfedaa libsystem_pthread.dylib`_pthread_start + 125
    frame #59: 0x00007fff6edfb6af libsystem_pthread.dylib`thread_start + 15

And the reason is that HTTPClient.resolve() computes a negative NIODeadline which is not legal (UInt64)

@weissi
Copy link
Contributor

weissi commented Jul 17, 2019

I'd say this is a NIO bug

@weissi
Copy link
Contributor

weissi commented Jul 17, 2019

I will fix this

weissi added a commit to weissi/swift-nio that referenced this issue Jul 17, 2019
Motivation:

Subtracting deadlines should just work, no matter if the result is
positive or negative.

Modifications:

Don't crash on earlierDeadline - laterDeadline.

Result:

- fewer crashes
- fixes swift-server/async-http-client#71
Lukasa pushed a commit to apple/swift-nio that referenced this issue Jul 19, 2019
Motivation:

Subtracting deadlines should just work, no matter if the result is
positive or negative.

Modifications:

Don't crash on earlierDeadline - laterDeadline.

Result:

- fewer crashes
- fixes swift-server/async-http-client#71
@weissi
Copy link
Contributor

weissi commented Jul 31, 2019

This is released btw as NIO 1.5.1 I believe

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants