vapor / websocket-kit

WebSocket client library built on SwiftNIO
https://docs.vapor.codes/4.0/advanced/websockets/
MIT License
272 stars 79 forks source link

crashed #150

Closed jugu1991 closed 5 months ago

jugu1991 commented 5 months ago

Describe the issue

Runtime crashes in vapor project

Vapor version

4.89.3

Operating system and version

ubuntu20.04

Swift version

Swift version 5.9.2 (swift-5.9.2-RELEASE)

Steps to reproduce

Crash when running in the vapor project, unable to reproduce immediately, it may be sporadic

Outcome

Vapor project crashes, stops running, cpu and memory usage are not high

Additional notes

Crash log:

*** Signal 11: Backtracing from 0x558a9022b860... done ***

*** Program crashed: Bad pointer dereference at 0x0000558a9022b860 ***

Thread 0 "nlx_6":

0  0x00007f592d1df48a __sigsuspend + 74 in libc-2.31.so

Thread 1 "NIO-ELT-0-#0":

0                0x00007f592d2bb68e epoll_wait + 94 in libc-2.31.so
1 [ra] [system]  0x000055868fff9411 specialized Selector.whenReady0(strategy:onLoopBegin:_:) + 624 in nlx_6 at /root/nlx_6/x-bot/
2 [ra] [inlined] 0x000055868fff5eba closure #2 in SelectableEventLoop.run() in nlx_6 at .build/checkouts/swift-nio/Sources/NIOPosix/SelectorGeneric.swift:288:18
3 [ra] [inlined] 0x000055868fff5eba specialized withAutoReleasePool(_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:29:16
4 [ra]           0x000055868fff5eba SelectableEventLoop.run() + 521 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:469:17
5 [ra] [inlined] 0x000055868ffceec3 static MultiThreadedEventLoopGroup.runTheLoop(thread:parentGroup:canEventLoopBeShutdownIndividually:selectorFactory:initializer:_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:93:22
6 [ra]           0x000055868ffceec3 closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 322 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:111:41
7 [ra] [thunk]   0x000055868ffd24da partial apply for closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 41 in nlx_6 at /root/nlx_6/x-bot/
8 [ra] [thunk]   0x000055868ffd49ff thunk for @escaping @callee_guaranteed (@guaranteed NIOThread) -> () + 14 in nlx_6 at /root/nlx_6/x-bot/
9 [ra]           0x0000558690011ecd closure #1 in static ThreadOpsPosix.run(handle:args:detachThread:) + 380 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/ThreadPosix.swift:116:13

Thread 2 "NIO-ELT-0-#1" crashed:

 0                         0x0000558a9022b860
 1 [ra]                    0x00005586901ea950 closure #5 in WebSocket.handle(incoming:) + 543 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/websocket-kit/Sources/WebSocketKit/WebSocket.swift:299:41
 2 [ra] [inlined] [system] 0x00005586901ea6d3 specialized NIOLockedValueBox.withLockedValue(_:) in nlx_6 at /root/nlx_6/x-bot/
 3 [ra]                    0x00005586901ea6d3 WebSocket.handle(incoming:) + 2962 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/websocket-kit/Sources/WebSocketKit/WebSocket.swift:293:28
 4 [ra] [inlined]          0x00005586901f4539 WebSocketHandler.channelRead(context:data:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/websocket-kit/Sources/WebSocketKit/WebSocketHandler.swift:131:24
 5 [ra] [thunk]            0x00005586901f4539 protocol witness for _ChannelInboundHandler.channelRead(context:data:) in conformance WebSocketHandler + 88 in nlx_6 at /root/nlx_6/x-bot/:129:10
 6 [ra]                    0x000055868fdfe0e6 ChannelHandlerContext.invokeChannelRead(_:) + 37 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1696:28
 7 [ra]                    0x000055868fdfb3f3 ChannelHandlerContext.fireChannelRead(_:) + 34 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1509:20
 8 [ra] [system]           0x00005586900606f1 NIOWebSocketFrameAggregator.channelRead(context:data:) + 288 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOWebSocket/NIOWebSocketFrameAggregator.swift
 9 [ra]                    0x000055868fdfe0e6 ChannelHandlerContext.invokeChannelRead(_:) + 37 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1696:28
10 [ra] [inlined]          0x000055868fdf1f33 ChannelHandlerContext.fireChannelRead(_:) in nlx_6 at .build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1509:20
11 [ra]                    0x000055868fdf1f33 _ChannelInboundHandler.channelRead(context:data:) + 34 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelHandler.swift:292:17
12 [ra]                    0x000055868fdfe0e6 ChannelHandlerContext.invokeChannelRead(_:) + 37 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1696:28
13 [ra]                    0x000055868fdfb3f3 ChannelHandlerContext.fireChannelRead(_:) + 34 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1509:20
14 [ra]                    0x0000558690067314 WebSocketFrameDecoder.decode(context:buffer:) + 227 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOWebSocket/WebSocketFrameDecoder.swift:261:25
15 [ra] [thunk]            0x00005586900673b0 protocol witness for ByteToMessageDecoder.decode(context:buffer:) in conformance WebSocketFrameDecoder + 15 in nlx_6 at /root/nlx_6/x-bot/
16 [ra]                    0x000055868fe0c337 closure #1 in ByteToMessageHandler.decodeLoop(context:decodeMode:) + 54 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/Codec.swift:580:49
17 [ra] [inlined]          0x000055868fe0c0f3 ByteToMessageHandler.withNextBuffer(allowEmptyBuffer:_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/Codec.swift:537:36
18 [ra]                    0x000055868fe0c0f3 ByteToMessageHandler.decodeLoop(context:decodeMode:) + 1202 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/Codec.swift:576:35
19 [ra]                    0x000055868fe0c951 ByteToMessageHandler.channelRead(context:data:) + 240 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/Codec.swift:649:29
20 [ra]                    0x000055868fdfe0e6 ChannelHandlerContext.invokeChannelRead(_:) + 37 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1696:28
21 [ra]                    0x000055868fdfe104 ChannelHandlerContext.invokeChannelRead(_:) + 67 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1698:24
22 [ra]                    0x000055868fdfb3f3 ChannelHandlerContext.fireChannelRead(_:) + 34 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1509:20
23 [ra]                    0x000055869002968c NIOSSLHandler.doFlushReadData(context:receiveBuffer:readOnEmptyBuffer:) + 171 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio-ssl/Sources/NIOSSL/NIOSSLHandler.swift:646:21
24 [ra]                    0x00005586900270be NIOSSLHandler.channelReadComplete(context:) + 93 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio-ssl/Sources/NIOSSL/NIOSSLHandler.swift:193:14
25 [ra]                    0x000055868fdfe130 ChannelHandlerContext.invokeChannelReadComplete() + 31 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOCore/ChannelPipeline.swift:1706:28
26 [ra]                    0x000055868fdfef76 ChannelPipeline.SynchronousOperations.fireChannelRegistered() + 37 in nlx_6
27 [ra]                    0x000055868fff59e1 specialized BaseSocketChannel.readable0() + 592 in nlx_6
28 [ra] [inlined]          0x000055868fffc96a specialized BaseSocketChannel.readable() in nlx_6 at .build/checkouts/swift-nio/Sources/NIOPosix/BaseSocketChannel.swift:1099:14
29 [ra] [inlined] [system] 0x000055868fffc96a specialized protocol witness for SelectableChannel.readable() in conformance BaseSocketChannel in nlx_6 at /root/nlx_6/x-bot/:1096:23
30 [ra]                    0x000055868fffc96a specialized SelectableEventLoop.handleEvent(_:channel:) + 153 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:403:25
31 [ra] [inlined] [system] 0x000055868fff7ef0 specialized SelectableEventLoop.handleEvent(_:channel:) in nlx_6 at /root/nlx_6/x-bot/
32 [ra]                    0x000055868fff7ef0 closure #2 in closure #2 in SelectableEventLoop.run() + 159 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:478:30
33 [ra] [thunk]            0x000055868ffffe4c partial apply for closure #2 in closure #2 in SelectableEventLoop.run() + 11 in nlx_6 at /root/nlx_6/x-bot/
34 [ra]                    0x000055868fff95e3 specialized Selector.whenReady0(strategy:onLoopBegin:_:) + 1090 in nlx_6 at .build/checkouts/swift-nio/Sources/NIOPosix/SelectorEpoll.swift:252:25
35 [ra] [inlined]          0x000055868fff5eba closure #2 in SelectableEventLoop.run() in nlx_6 at .build/checkouts/swift-nio/Sources/NIOPosix/SelectorGeneric.swift:288:18
36 [ra] [inlined]          0x000055868fff5eba specialized withAutoReleasePool(_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:29:16
37 [ra]                    0x000055868fff5eba SelectableEventLoop.run() + 521 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:469:17
38 [ra] [inlined]          0x000055868ffceec3 static MultiThreadedEventLoopGroup.runTheLoop(thread:parentGroup:canEventLoopBeShutdownIndividually:selectorFactory:initializer:_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:93:22
39 [ra]                    0x000055868ffceec3 closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 322 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:111:41
40 [ra] [thunk]            0x000055868ffd24da partial apply for closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 41 in nlx_6 at /root/nlx_6/x-bot/
41 [ra] [thunk]            0x000055868ffd49ff thunk for @escaping @callee_guaranteed (@guaranteed NIOThread) -> () + 14 in nlx_6 at /root/nlx_6/x-bot/
42 [ra]                    0x0000558690011ecd closure #1 in static ThreadOpsPosix.run(handle:args:detachThread:) + 380 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/ThreadPosix.swift:116:13

Thread 3 "NIO-ELT-0-#2":

0                0x00007f592d2bb68e epoll_wait + 94 in libc-2.31.so
1 [ra] [system]  0x000055868fff9411 specialized Selector.whenReady0(strategy:onLoopBegin:_:) + 624 in nlx_6 at /root/nlx_6/x-bot/
2 [ra] [inlined] 0x000055868fff5eba closure #2 in SelectableEventLoop.run() in nlx_6 at .build/checkouts/swift-nio/Sources/NIOPosix/SelectorGeneric.swift:288:18
3 [ra] [inlined] 0x000055868fff5eba specialized withAutoReleasePool(_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:29:16
4 [ra]           0x000055868fff5eba SelectableEventLoop.run() + 521 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:469:17
5 [ra] [inlined] 0x000055868ffceec3 static MultiThreadedEventLoopGroup.runTheLoop(thread:parentGroup:canEventLoopBeShutdownIndividually:selectorFactory:initializer:_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:93:22
6 [ra]           0x000055868ffceec3 closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 322 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:111:41
7 [ra] [thunk]   0x000055868ffd24da partial apply for closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 41 in nlx_6 at /root/nlx_6/x-bot/
8 [ra] [thunk]   0x000055868ffd49ff thunk for @escaping @callee_guaranteed (@guaranteed NIOThread) -> () + 14 in nlx_6 at /root/nlx_6/x-bot/
9 [ra]           0x0000558690011ecd closure #1 in static ThreadOpsPosix.run(handle:args:detachThread:) + 380 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/ThreadPosix.swift:116:13

Thread 4 "NIO-ELT-0-#3":

0                0x00007f592d2bb68e epoll_wait + 94 in libc-2.31.so
1 [ra] [system]  0x000055868fff9411 specialized Selector.whenReady0(strategy:onLoopBegin:_:) + 624 in nlx_6 at /root/nlx_6/x-bot/
2 [ra] [inlined] 0x000055868fff5eba closure #2 in SelectableEventLoop.run() in nlx_6 at .build/checkouts/swift-nio/Sources/NIOPosix/SelectorGeneric.swift:288:18
3 [ra] [inlined] 0x000055868fff5eba specialized withAutoReleasePool(_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:29:16
4 [ra]           0x000055868fff5eba SelectableEventLoop.run() + 521 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/SelectableEventLoop.swift:469:17
5 [ra] [inlined] 0x000055868ffceec3 static MultiThreadedEventLoopGroup.runTheLoop(thread:parentGroup:canEventLoopBeShutdownIndividually:selectorFactory:initializer:_:) in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:93:22
6 [ra]           0x000055868ffceec3 closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 322 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift:111:41
7 [ra] [thunk]   0x000055868ffd24da partial apply for closure #1 in static MultiThreadedEventLoopGroup.setupThreadAndEventLoop(name:parentGroup:selectorFactory:initializer:) + 41 in nlx_6 at /root/nlx_6/x-bot/
8 [ra] [thunk]   0x000055868ffd49ff thunk for @escaping @callee_guaranteed (@guaranteed NIOThread) -> () + 14 in nlx_6 at /root/nlx_6/x-bot/
9 [ra]           0x0000558690011ecd closure #1 in static ThreadOpsPosix.run(handle:args:detachThread:) + 380 in nlx_6 at /root/nlx_6/x-bot/.build/checkouts/swift-nio/Sources/NIOPosix/ThreadPosix.swift:116:13

Thread 5 "TP-#0":

0  0x00007f592d5c6454 do_futex_wait.constprop.0 + 52 in libpthread-2.31.so

Thread 6 "TP-#1":

0  0x00007f592d5c6454 do_futex_wait.constprop.0 + 52 in libpthread-2.31.so

Thread 7 "TP-#2":

0  0x00007f592d5c6454 do_futex_wait.constprop.0 + 52 in libpthread-2.31.so

Thread 8 "TP-#3":

0  0x00007f592d5c6454 do_futex_wait.constprop.0 + 52 in libpthread-2.31.so

Thread 9:

0  0x00007f592d2bb68e epoll_wait + 94 in libc-2.31.so

Thread 10:

0  0x00007f592d5c6678 do_futex_wait.constprop.0 + 72 in libpthread-2.31.so

Thread 11:

0  0x00007f592d5c6678 do_futex_wait.constprop.0 + 72 in libpthread-2.31.so

Thread 12:

0  0x00007f592d5c6678 do_futex_wait.constprop.0 + 72 in libpthread-2.31.so

Thread 13:

0  0x00007f592d5c6678 do_futex_wait.constprop.0 + 72 in libpthread-2.31.so

Thread 14:

0  0x00007f592d5c6678 do_futex_wait.constprop.0 + 72 in libpthread-2.31.so

Registers:

rax 0x00007f5914437610  e0 aa 63 90 86 55 00 00 03 00 00 00 02 00 00 00  àªc··U··········
rdx 0x00007f591c30c300  58 9d 82 2e 59 7f 00 00 03 00 00 00 02 00 00 00  X··.Y···········
rcx 0xf0000000000000b8  17293822569102704824
rbx 0x00007f591c30c300  58 9d 82 2e 59 7f 00 00 03 00 00 00 02 00 00 00  X··.Y···········
rsi 0x00007f591c1afac0  68 85 65 90 86 55 00 00 b0 a0 82 23 eb 0f 00 c0  h·e··U··° ·#ë··À
rdi 0xf0000000000000b8  17293822569102704824
rbp 0x00007f592a98f140  10 f2 98 2a 59 7f 00 00 50 a9 1e 90 86 55 00 00  ·ò·*Y···P©···U··
rsp 0x00007f592a98f128  ce b2 1e 90 86 55 00 00 00 c3 30 1c 59 7f 00 00  β···U···Ã0·Y···
 r8 0x0000000000000001  1
 r9 0x0000000200000000  8589934592
r10 0x00007f591c1afac0  68 85 65 90 86 55 00 00 b0 a0 82 23 eb 0f 00 c0  h·e··U··° ·#ë··À
r11 0x0000000000000001  1
r12 0x0000000000000000  0
r13 0x0000000000000000  0
r14 0x00007f591c0cb310  50 4e 01 24 59 7f 00 00 00 00 00 00 00 00 00 00  PN·$Y···········
r15 0x00007f5924014e50  98 a5 64 90 86 55 00 00 03 00 00 00 12 00 00 00  ·¥d··U··········
rip 0x0000558a9022b860  94053612042336

rflags 0x0000000000010202  

cs 0x0033  fs 0x0000  gs 0x0000

Images (23 omitted):

0x000055868f754000–0x00005586905cbc68                             nlx_6              /root/nlx_6/x-bot/.build/x86_64-unknown-linux-gnu/release/nlx_6
0x00007f592d19c000–0x00007f592d335864 eebe5d5f4b608b8a53ec446b63981bba373ca0ca libc-2.31.so       /usr/lib/x86_64-linux-gnu/libc-2.31.so
0x00007f592d5b4000–0x00007f592d5ca545 0c044ba611aeeeaebb8374e660061f341ebc0bac libpthread-2.31.so /usr/lib/x86_64-linux-gnu/libpthread-2.31.so

Backtrace took 53.32s
0xTim commented 5 months ago

Is this reproducible and does it happen with Swift 5.10?

jugu1991 commented 5 months ago

@0xTim Cannot reproduce, also occurs sporadically in swift 5.9.2, possibly due to unexpected exceptions such as receiving messages that are not in JSON format.

0xTim commented 5 months ago

It shouldn't matter what format the messages are in, JSON messages are an application concern rather than framework concern.

The crash is definitely a Swift bug - even if there is a bug it shouldn't crash - so I'd recommend updating to the latest Swift version and see if that fixes it