ChrisRega / example-blog-client

MIT License
6 stars 1 forks source link

Nested event loops #6

Open parasyte opened 1 day ago

parasyte commented 1 day ago

This application nests the winit event loop inside the tokio event loop: https://github.com/ChrisRega/example-blog-client/blob/7b42b9aef9820f5a66f8680854046cc7835a8bd5/src/main.rs#L17-L26

Doing this is undefined behavior. It causes serious problems on platforms like macOS, and blocks one of the tokio worker threads. This flavor of UB is visible on macOS where the app will crash with an uncaught exception when closing the window:

Trace/BPT trap: 5 ``` Process: example_blog_client [88174] Path: /Users/USER/*/example_blog_client Identifier: example_blog_client Version: ??? Code Type: ARM-64 (Native) Parent Process: bash [21143] Responsible: Terminal [7005] User ID: 501 Date/Time: 2024-10-23 07:32:37.4558 -0700 OS Version: macOS 14.6.1 (23G93) Report Version: 12 Anonymous UUID: CFCF464F-AF6C-A205-19C6-73C7EBE19B5C Sleep/Wake UUID: F6360395-3827-4373-A55D-758348322FE5 Time Awake Since Boot: 1000000 seconds Time Since Wake: 329 seconds System Integrity Protection: enabled Crashed Thread: 0 main Dispatch queue: com.apple.main-thread Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x000000018e10f758 Termination Reason: Namespace SIGNAL, Code 5 Trace/BPT trap: 5 Terminating Process: exc handler [88174] Application Specific Backtrace 0: 0 CoreFoundation 0x000000018a64a2ec __exceptionPreprocess + 176 1 libobjc.A.dylib 0x000000018a12e158 objc_exception_throw + 60 2 Foundation 0x000000018b6fc36c -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 628 3 Foundation 0x000000018b6fc0a4 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 136 4 Foundation 0x000000018b6fbd70 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:context:] + 196 5 AppKit 0x000000018e5393e4 -[_NSTouchBarFinderObservation invalidate] + 264 6 AppKit 0x000000018e53a738 ___NSTouchBarFinderSetNeedsUpdateOnMain_block_invoke_2 + 628 7 AppKit 0x000000018ded7cec NSDisplayCycleObserverInvoke + 168 8 AppKit 0x000000018ded7948 NSDisplayCycleFlush + 644 9 QuartzCore 0x00000001927ca98c _ZN2CA11Transaction19run_commit_handlersE18CATransactionPhase + 120 10 QuartzCore 0x00000001927c972c _ZN2CA11Transaction6commitEv + 320 11 AppKit 0x000000018df5bac8 __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 272 12 AppKit 0x000000018e91c4e4 ___NSRunLoopObserverCreateWithHandler_block_invoke + 64 13 CoreFoundation 0x000000018a5d487c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36 14 CoreFoundation 0x000000018a5d4768 __CFRunLoopDoObservers + 536 15 CoreFoundation 0x000000018a5d3d94 __CFRunLoopRun + 776 16 CoreFoundation 0x000000018a5d3434 CFRunLoopRunSpecific + 608 17 HIToolbox 0x0000000194d7d19c RunCurrentEventLoopInMode + 292 18 HIToolbox 0x0000000194d7ce2c ReceiveNextEventCommon + 220 19 HIToolbox 0x0000000194d7cd30 _BlockUntilNextEventMatchingListInModeWithFilter + 76 20 AppKit 0x000000018de32cc8 _DPSNextEvent + 660 21 AppKit 0x000000018e6294d0 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 700 22 AppKit 0x000000018de25ffc -[NSApplication run] + 476 23 example_blog_client 0x000000010216cc60 _ZN5winit13platform_impl8platform10event_loop18EventLoop$LT$T$GT$10run_return17hd204e57310b01024E + 380 24 example_blog_client 0x000000010217ae00 _ZN6eframe6native3run14run_and_return17h98b04053861a1c38E + 572 25 example_blog_client 0x000000010216b268 _ZN6eframe6native3run16glow_integration8run_glow17h73e6eff3e463615aE + 596 26 example_blog_client 0x000000010217b29c _ZN6eframe10run_native17hf2ac08c50c3f68a8E + 428 27 example_blog_client 0x0000000102075a14 _ZN5tokio7runtime4park16CachedParkThread8block_on17hfbfd26e6bada6b29E + 224 28 example_blog_client 0x00000001020796e4 _ZN5tokio7runtime7runtime7Runtime8block_on17h6ea9cf69a2a8ee76E + 88 29 example_blog_client 0x0000000102052dbc _ZN19example_blog_client4main17h0eddff21d4633986E + 112 30 example_blog_client 0x0000000102077ba4 _ZN3std3sys9backtrace28__rust_begin_short_backtrace17h1f48f483b07c3691E + 12 31 example_blog_client 0x0000000102074398 _ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h9a6c470f04bee322E.llvm.18315569416295985373 + 16 32 example_blog_client 0x00000001023bbdc0 _ZN3std2rt19lang_start_internal17h738180e634528c71E + 812 33 example_blog_client 0x0000000102052e7c main + 52 34 dyld 0x000000018a16b154 start + 2476 Thread 0 Crashed:: main Dispatch queue: com.apple.main-thread 0 AppKit 0x18e10f758 -[NSApplication _crashOnException:] + 240 1 AppKit 0x18df5bc3c __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 644 2 AppKit 0x18e91c4e4 ___NSRunLoopObserverCreateWithHandler_block_invoke + 64 3 CoreFoundation 0x18a5d487c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36 4 CoreFoundation 0x18a5d4768 __CFRunLoopDoObservers + 536 5 CoreFoundation 0x18a5d3d94 __CFRunLoopRun + 776 6 CoreFoundation 0x18a5d3434 CFRunLoopRunSpecific + 608 7 HIToolbox 0x194d7d19c RunCurrentEventLoopInMode + 292 8 HIToolbox 0x194d7ce2c ReceiveNextEventCommon + 220 9 HIToolbox 0x194d7cd30 _BlockUntilNextEventMatchingListInModeWithFilter + 76 10 AppKit 0x18de32cc8 _DPSNextEvent + 660 11 AppKit 0x18e6294d0 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 700 12 AppKit 0x18de25ffc -[NSApplication run] + 476 13 example_blog_client 0x10216cc60 winit::platform_impl::platform::event_loop::EventLoop$LT$T$GT$::run_return::hd204e57310b01024 + 380 14 example_blog_client 0x10217ae00 eframe::native::run::run_and_return::h98b04053861a1c38 + 572 15 example_blog_client 0x10216b268 eframe::native::run::glow_integration::run_glow::h73e6eff3e463615a + 596 16 example_blog_client 0x10217b29c eframe::run_native::hf2ac08c50c3f68a8 + 428 17 example_blog_client 0x102075a14 tokio::runtime::park::CachedParkThread::block_on::hfbfd26e6bada6b29 + 224 18 example_blog_client 0x1020796e4 tokio::runtime::runtime::Runtime::block_on::h6ea9cf69a2a8ee76 + 88 19 example_blog_client 0x102052dbc example_blog_client::main::h0eddff21d4633986 + 112 20 example_blog_client 0x102077ba4 std::sys::backtrace::__rust_begin_short_backtrace::h1f48f483b07c3691 + 12 21 example_blog_client 0x102074398 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h9a6c470f04bee322 (.llvm.18315569416295985373) + 16 22 example_blog_client 0x1023bbdc0 std::rt::lang_start_internal::h738180e634528c71 + 812 23 example_blog_client 0x102052e7c main + 52 24 dyld 0x18a16b154 start + 2476 Thread 1:: tokio-runtime-worker 0 libsystem_kernel.dylib 0x18a4baf40 kevent + 8 1 example_blog_client 0x1023a2b1c mio::poll::Poll::poll::h2e4310d4ea0fbade + 116 2 example_blog_client 0x10238fe98 tokio::runtime::io::Driver::turn::hf0008c1ab73a6f72 (.llvm.16663742601449969147) + 88 3 example_blog_client 0x102390660 tokio::runtime::time::Driver::park_internal::hd799af5bbfe1604b (.llvm.16663742601449969147) + 688 4 example_blog_client 0x10238aa80 tokio::runtime::scheduler::multi_thread::park::Parker::park::hd1c8149a5a640831 + 440 5 example_blog_client 0x1023853d4 tokio::runtime::scheduler::multi_thread::worker::Context::park_timeout::hd9d677c8cbb051cc + 152 6 example_blog_client 0x102384c58 tokio::runtime::scheduler::multi_thread::worker::Context::run::h67dd108fa651a15a + 1260 7 example_blog_client 0x1023962b8 tokio::macros::scoped_tls::ScopedKey$LT$T$GT$::set::hc5fbf11fa4d71847 + 84 8 example_blog_client 0x102384668 tokio::runtime::scheduler::multi_thread::worker::run::h17700621f5c59ec3 + 132 9 example_blog_client 0x10238595c _$LT$tokio..runtime..blocking..task..BlockingTask$LT$T$GT$$u20$as$u20$core..future..future..Future$GT$::poll::h7ef90a31b5cc97ca + 80 10 example_blog_client 0x10239a6c4 tokio::runtime::task::core::Core$LT$T$C$S$GT$::poll::h6b1ea7dfeb277b85 + 148 11 example_blog_client 0x1023913c4 tokio::runtime::task::harness::Harness$LT$T$C$S$GT$::poll::h36aa34a38b3efdb2 + 176 12 example_blog_client 0x10238bf70 tokio::runtime::blocking::pool::Inner::run::h2cb318c4b884e453 + 408 13 example_blog_client 0x10238108c std::sys::backtrace::__rust_begin_short_backtrace::hd9606544b2d39132 + 80 14 example_blog_client 0x102399284 core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h886c10b4a08115c0 + 188 15 example_blog_client 0x1023c5214 std::sys::pal::unix::thread::Thread::new::thread_start::h7f24d7d921e6556e + 52 16 libsystem_pthread.dylib 0x18a4f5f94 _pthread_start + 136 17 libsystem_pthread.dylib 0x18a4f0d34 thread_start + 8 Thread 2: 0 libsystem_pthread.dylib 0x18a4f0d20 start_wqthread + 0 Thread 3: 0 libsystem_pthread.dylib 0x18a4f0d20 start_wqthread + 0 Thread 4: 0 libsystem_pthread.dylib 0x18a4f0d20 start_wqthread + 0 Thread 5: 0 libsystem_pthread.dylib 0x18a4f0d20 start_wqthread + 0 Thread 6:: tokio-runtime-worker 0 libsystem_kernel.dylib 0x18a4b85ec __psynch_cvwait + 8 1 libsystem_pthread.dylib 0x18a4f655c _pthread_cond_wait + 1228 2 example_blog_client 0x1023c5c04 std::sys::sync::condvar::pthread::Condvar::wait_timeout::h9c14fc6b89193d63 + 256 3 example_blog_client 0x10238bea8 tokio::runtime::blocking::pool::Inner::run::h2cb318c4b884e453 + 208 4 example_blog_client 0x10238108c std::sys::backtrace::__rust_begin_short_backtrace::hd9606544b2d39132 + 80 5 example_blog_client 0x102399284 core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h886c10b4a08115c0 + 188 6 example_blog_client 0x1023c5214 std::sys::pal::unix::thread::Thread::new::thread_start::h7f24d7d921e6556e + 52 7 libsystem_pthread.dylib 0x18a4f5f94 _pthread_start + 136 8 libsystem_pthread.dylib 0x18a4f0d34 thread_start + 8 Thread 7:: tokio-runtime-worker 0 libsystem_kernel.dylib 0x18a4b85ec __psynch_cvwait + 8 1 libsystem_pthread.dylib 0x18a4f655c _pthread_cond_wait + 1228 2 example_blog_client 0x1023c5c04 std::sys::sync::condvar::pthread::Condvar::wait_timeout::h9c14fc6b89193d63 + 256 3 example_blog_client 0x10238bea8 tokio::runtime::blocking::pool::Inner::run::h2cb318c4b884e453 + 208 4 example_blog_client 0x10238108c std::sys::backtrace::__rust_begin_short_backtrace::hd9606544b2d39132 + 80 5 example_blog_client 0x102399284 core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h886c10b4a08115c0 + 188 6 example_blog_client 0x1023c5214 std::sys::pal::unix::thread::Thread::new::thread_start::h7f24d7d921e6556e + 52 7 libsystem_pthread.dylib 0x18a4f5f94 _pthread_start + 136 8 libsystem_pthread.dylib 0x18a4f0d34 thread_start + 8 Thread 8:: com.apple.NSEventThread 0 libsystem_kernel.dylib 0x18a4b4df4 mach_msg2_trap + 8 1 libsystem_kernel.dylib 0x18a4c75e4 mach_msg2_internal + 80 2 libsystem_kernel.dylib 0x18a4bd9c4 mach_msg_overwrite + 476 3 libsystem_kernel.dylib 0x18a4b5178 mach_msg + 24 4 CoreFoundation 0x18a5d5680 __CFRunLoopServiceMachPort + 160 5 CoreFoundation 0x18a5d3f44 __CFRunLoopRun + 1208 6 CoreFoundation 0x18a5d3434 CFRunLoopRunSpecific + 608 7 AppKit 0x18df5d280 _NSEventThread + 144 8 libsystem_pthread.dylib 0x18a4f5f94 _pthread_start + 136 9 libsystem_pthread.dylib 0x18a4f0d34 thread_start + 8 Thread 0 crashed with ARM Thread State (64-bit): x0: 0x000000013f945400 x1: 0x000000013f94ad39 x2: 0xfffffffffffffff8 x3: 0x000000013f946318 x4: 0x0000000000004a01 x5: 0x0000000000000020 x6: 0x000000013f945400 x7: 0x0000000000000f58 x8: 0x00000001f28a5000 x9: 0x8ba4586afe9700d6 x10: 0x8ba4586afe9700d6 x11: 0x000000000000007f x12: 0x0000000000000017 x13: 0x000000013f926960 x14: 0x01000001f235c589 x15: 0x00000001f235c588 x16: 0x000000018a5261a0 x17: 0x00000001fca42aa8 x18: 0x0000000000000000 x19: 0x00006000031452f0 x20: 0x0000600002a09540 x21: 0x0000168d57ab2854 x22: 0x000000013f009050 x23: 0x000000016ddbfec0 x24: 0x0000000000000000 x25: 0x0000000000000000 x26: 0x0000600000e1d360 x27: 0x0000600003144f00 x28: 0x000000018a5d4e04 fp: 0x000000016ddbfd30 lr: 0x102e80018e10f750 sp: 0x000000016ddbfd00 pc: 0x000000018e10f758 cpsr: 0x60001000 far: 0x0000000000000000 esr: 0xf2000001 (Breakpoint) brk 1 Binary Images: 0x10cfd4000 - 0x10cfdffff libobjc-trampolines.dylib (*) <7d6ca1cb-c5e6-38b6-9618-2c580486fc6b> /usr/lib/libobjc-trampolines.dylib 0x102da0000 - 0x102e0bfff com.apple.AppleMetalOpenGLRenderer (1.0) /System/Library/Extensions/AppleMetalOpenGLRenderer.bundle/Contents/MacOS/AppleMetalOpenGLRenderer 0x10203c000 - 0x10277bfff example_blog_client (*) <4c4c442c-5555-3144-a146-ed08b13d75d6> /Users/USER/*/example_blog_client 0x18ddf9000 - 0x18f137fff com.apple.AppKit (6.9) <7acbeef9-a453-3e4d-98ac-f9441d86e183> /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit 0x18a557000 - 0x18aa2ffff com.apple.CoreFoundation (6.9) <55720ee0-d8c3-33a1-9854-8e61a6f44895> /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation 0x194d4a000 - 0x19500dfff com.apple.HIToolbox (2.1.1) <4166f6cb-b599-306c-8568-823bd29a74c3> /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox 0x18a165000 - 0x18a1ee507 dyld (*) /usr/lib/dyld 0x0 - 0xffffffffffffffff ??? (*) <00000000-0000-0000-0000-000000000000> ??? 0x18a522000 - 0x18a529fe7 libsystem_platform.dylib (*) /usr/lib/system/libsystem_platform.dylib 0x18a4b4000 - 0x18a4eeffb libsystem_kernel.dylib (*) <71ff45b8-f14e-3666-9e96-6cf58315b91d> /usr/lib/system/libsystem_kernel.dylib 0x18a4ef000 - 0x18a4fbfff libsystem_pthread.dylib (*) /usr/lib/system/libsystem_pthread.dylib External Modification Summary: Calls made by other processes targeting this process: task_for_pid: 0 thread_create: 0 thread_set_state: 0 Calls made by this process: task_for_pid: 0 thread_create: 0 thread_set_state: 0 Calls made by all processes on this machine: task_for_pid: 0 thread_create: 0 thread_set_state: 0 VM Region Summary: ReadOnly portion of Libraries: Total=837.7M resident=0K(0%) swapped_out_or_unallocated=837.7M(100%) Writable regions: Total=2.3G written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=2.3G(100%) VIRTUAL REGION REGION TYPE SIZE COUNT (non-coalesced) =========== ======= ======= Accelerate framework 128K 1 Activity Tracing 256K 1 CG image 160K 7 ColorSync 624K 29 CoreAnimation 336K 16 CoreGraphics 32K 2 CoreUI image data 1248K 9 Foundation 16K 1 Kernel Alloc Once 32K 1 MALLOC 2.3G 72 MALLOC guard page 192K 12 OpenGL GLSL 256K 3 STACK GUARD 128K 8 Stack 16.7M 9 Stack Guard 56.0M 1 VM_ALLOCATE 704K 30 VM_ALLOCATE (reserved) 512K 4 reserved VM address space (unallocated) __AUTH 940K 207 __AUTH_CONST 15.0M 368 __CTF 824 1 __DATA 3596K 356 __DATA_CONST 18.9M 372 __DATA_DIRTY 924K 116 __FONT_DATA 2352 1 __GLSLBUILTINS 5174K 1 __LINKEDIT 527.7M 4 __OBJC_RO 71.9M 1 __OBJC_RW 2200K 1 __TEXT 310.0M 386 dyld private memory 272K 2 mapped file 179.5M 25 shared memory 1392K 15 =========== ======= ======= TOTAL 3.5G 2062 TOTAL, minus reserved VM space 3.5G 2062 ```

Further reading:

ChrisRega commented 22 hours ago

Thanks for the report!