dpc / mioco.pre-0.9

Scalable, coroutine-based, asynchronous IO handling library for Rust programming language. (aka MIO COroutines).
Mozilla Public License 2.0
457 stars 30 forks source link

Channel panics when mixing `recv()` and `select!` #134

Closed SAPikachu closed 8 years ago

SAPikachu commented 8 years ago

Test case:

#![allow(dead_code)]

#[macro_use] extern crate mioco;

use mioco::sync::mpsc::{channel};

fn main() {
    mioco::start(move || {
        let (send1, recv1) = channel::<i32>();
        let (send2, recv2) = channel::<i32>();
        let (send3, recv3) = channel::<i32>();
        let handle = mioco::spawn(move || {
            loop {
                println!("Received (1): {}", recv1.recv().unwrap());
                select!(
                    r:recv2 => {
                        println!("Received (2): {:?}", recv2.try_recv());
                    },
                    r:recv3 => {
                        println!("Received (3): {:?}", recv3.try_recv());
                    },
                );
            }
        });
        let handle2 = mioco::spawn(move || {
            send1.send(42).unwrap();
            send2.send(43).unwrap();
            send3.send(44).unwrap();
            send1.send(45).unwrap();
            send2.send(46).unwrap();
            send3.send(47).unwrap();
            handle.join().unwrap();
        });
        handle2.join().unwrap();
    }).unwrap();
}

Using master branch of mioco (58ff165) to compile, cargo run outputs following:

Received (1): 42
Received (2): Ok(43)
Received (1): 45
Received (3): Ok(44)
thread '<main>' panicked at 'assertion failed: coroutine.last_event.id().as_usize() ==
    self.shared().0.borrow().common.id.unwrap().as_usize()', /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/evented.rs:58
note: Run with `RUST_BACKTRACE=1` for a backtrace.
thread 'mioco_thread_1' panicked at 'called `Result::unwrap()` on an `Err` value: Any', ../src/libcore/result.rs:785
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Any', ../src/libcore/result.rs:785
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Any', ../src/libcore/result.rs:785
error: Process didn't exit successfully: `target/debug/mioco_test` (exit code: 101)
SAPikachu commented 8 years ago

Output with backtraces:

Received (1): 42
Received (2): Ok(43)
Received (1): 45
Received (3): Ok(44)
thread '<main>' panicked at 'assertion failed: coroutine.last_event.id().as_usize() ==
    self.shared().0.borrow().common.id.unwrap().as_usize()', /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/evented.rs:58
stack backtrace:
   1:     0x55e178adace0 - std::sys::backtrace::tracing::imp::write::h9fb600083204ae7f
   2:     0x55e178add94b - std::panicking::default_hook::_$u7b$$u7b$closure$u7d$$u7d$::hca543c34f11229ac
   3:     0x55e178add5d3 - std::panicking::default_hook::hc2c969e7453d080c
   4:     0x55e178ad461f - std::sys_common::unwind::begin_unwind_inner::h30e12d15ce2b2e25
   5:     0x55e178a48857 - std::sys_common::unwind::begin_unwind::h65390c63b501d6ed
                        at ../src/libstd/sys/common/unwind/mod.rs:219
   6:     0x55e178a94ccd - mioco::src::evented::EventedImpl::block_on_prv::hc0a4745947fb598e
                        at /home/sapikachu/Dropbox/devel/sandbox/mioco_test/<std macros>:3
   7:     0x55e178a9445b - _<EP as mioco..Evented>::block_on::h69b14ad99a0b0831
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/evented.rs:239
   8:     0x55e178a915f2 - _<mioco..sync..mpsc..Receiver<T>>::recv::h8ea4caec99b0d882
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/sync/mpsc.rs:113
   9:     0x55e178a8f463 - mioco_test::main::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h9836888d88c96d57
                        at src/lib.rs:14
  10:     0x55e178a8f1b5 - _<std..panic..AssertUnwindSafe<F> as std..ops..FnOnce<()>>::call_once::hb7a778b33cf90ff1
                        at ../src/libstd/panic.rs:284
  11:     0x55e178a8f154 - mioco::src::coroutine::Coroutine::spawn::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::hd2d9fac55afc4735
  12:     0x55e178a8ec5c - std::panic::catch_unwind::_$u7b$$u7b$closure$u7d$$u7d$::h5458387c6b5b95e0
  13:     0x55e178a8ec09 - std::sys_common::unwind::try::try_fn::hc6e2b8c87047abcf
                        at ../src/libstd/sys/common/unwind/mod.rs:127
  14:     0x55e178ad9ebb - __rust_try
  15:     0x55e178ad9e4d - std::sys_common::unwind::inner_try::h47a4d9cd4a369dcd
  16:     0x55e178a8eb77 - std::sys_common::unwind::try::h3d61159730a1dbd8
                        at ../src/libstd/sys/common/unwind/mod.rs:123
  17:     0x55e178a8e959 - std::panic::catch_unwind::h3396eb65f63e4448
                        at ../src/libstd/panic.rs:389
  18:     0x55e178a8d8e1 - mioco::src::coroutine::Coroutine::spawn::_$u7b$$u7b$closure$u7d$$u7d$::h14d1f386de3b420d
  19:     0x55e178a90802 - _<mioco..src..thunk..Thunk<'a, (), R>>::new::_$u7b$$u7b$closure$u7d$$u7d$::h01181225461e9b0b
  20:     0x55e178a9073d - _<F as mioco..src..thunk..Invoke<A, R>>::invoke::hd71ade245af0a843
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/thunk.rs:47
  21:     0x55e178ac2d12 - _<src..thunk..Thunk<'a, A, R>>::invoke::h2660317a668d81e2
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/thunk.rs:33
  22:     0x55e178ac24c5 - mioco::src::coroutine::Coroutine::spawn::coroutine_context_start_fn::hb2ad76ddad0055cd
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/coroutine.rs:223
  23:     0x55e178acbc5a - <unknown>
                        at src/asm/make_x86_64_sysv_elf_gas.S:51
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Any', ../src/libcore/result.rs:785
stack backtrace:
   1:     0x55e178adace0 - std::sys::backtrace::tracing::imp::write::h9fb600083204ae7f
   2:     0x55e178add94b - std::panicking::default_hook::_$u7b$$u7b$closure$u7d$$u7d$::hca543c34f11229ac
   3:     0x55e178add5d3 - std::panicking::default_hook::hc2c969e7453d080c
   4:     0x55e178ad461f - std::sys_common::unwind::begin_unwind_inner::h30e12d15ce2b2e25
   5:     0x55e178ad51c8 - std::sys_common::unwind::begin_unwind_fmt::hb2de8a9968d38523
   6:     0x55e178ad9f31 - rust_begin_unwind
   7:     0x55e178b12d4f - core::panicking::panic_fmt::h257ceb0aa351d801
   8:     0x55e178a431d0 - core::result::unwrap_failed::h2cc106e731b24162
                        at ../src/libcore/macros.rs:29
   9:     0x55e178a43000 - _<std..result..Result<T, E>>::unwrap::hc95910105c6ee0ce
                        at ../src/libcore/result.rs:723
  10:     0x55e178aa33e4 - mioco_test::main::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h17d58ac533fe4239
                        at src/lib.rs:32
  11:     0x55e178aa30d5 - _<std..panic..AssertUnwindSafe<F> as std..ops..FnOnce<()>>::call_once::hcea4b89a913ba87d
                        at ../src/libstd/panic.rs:284
  12:     0x55e178aa2fe0 - mioco::src::coroutine::Coroutine::spawn::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h505b8f0ace9cfcbd
  13:     0x55e178aa281d - std::panic::catch_unwind::_$u7b$$u7b$closure$u7d$$u7d$::h2f345d7bfa74a444
  14:     0x55e178aa2762 - std::sys_common::unwind::try::try_fn::hb707c1af3945e539
                        at ../src/libstd/sys/common/unwind/mod.rs:127
  15:     0x55e178ad9ebb - __rust_try
  16:     0x55e178ad9e4d - std::sys_common::unwind::inner_try::h47a4d9cd4a369dcd
  17:     0x55e178aa26aa - std::sys_common::unwind::try::h58473caa516eb7d4
                        at ../src/libstd/sys/common/unwind/mod.rs:123
  18:     0x55e178aa23cf - std::panic::catch_unwind::h08e20b1e952ce797
                        at ../src/libstd/panic.rs:389
  19:     0x55e178aa12c5 - mioco::src::coroutine::Coroutine::spawn::_$u7b$$u7b$closure$u7d$$u7d$::h69d2bfe1cd6c857d
  20:     0x55e178aa414f - _<mioco..src..thunk..Thunk<'a, (), R>>::new::_$u7b$$u7b$closure$u7d$$u7d$::hebd1450507818421
  21:     0x55e178aa408c - _<F as mioco..src..thunk..Invoke<A, R>>::invoke::h9d9ad5bd743bd381
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/thunk.rs:47
  22:     0x55e178ac2d12 - _<src..thunk..Thunk<'a, A, R>>::invoke::h2660317a668d81e2
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/thunk.rs:33
  23:     0x55e178ac24c5 - mioco::src::coroutine::Coroutine::spawn::coroutine_context_start_fn::hb2ad76ddad0055cd
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/coroutine.rs:223
  24:     0x55e178acbc5a - <unknown>
                        at src/asm/make_x86_64_sysv_elf_gas.S:51
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Any', ../src/libcore/result.rs:785
stack backtrace:
   1:     0x55e178adace0 - std::sys::backtrace::tracing::imp::write::h9fb600083204ae7f
   2:     0x55e178add94b - std::panicking::default_hook::_$u7b$$u7b$closure$u7d$$u7d$::hca543c34f11229ac
   3:     0x55e178add5d3 - std::panicking::default_hook::hc2c969e7453d080c
   4:     0x55e178ad461f - std::sys_common::unwind::begin_unwind_inner::h30e12d15ce2b2e25
   5:     0x55e178ad51c8 - std::sys_common::unwind::begin_unwind_fmt::hb2de8a9968d38523
   6:     0x55e178ad9f31 - rust_begin_unwind
   7:     0x55e178b12d4f - core::panicking::panic_fmt::h257ceb0aa351d801
   8:     0x55e178a431d0 - core::result::unwrap_failed::h2cc106e731b24162
                        at ../src/libcore/macros.rs:29
   9:     0x55e178a43000 - _<std..result..Result<T, E>>::unwrap::hc95910105c6ee0ce
                        at ../src/libcore/result.rs:723
  10:     0x55e178a68061 - mioco_test::main::_$u7b$$u7b$closure$u7d$$u7d$::h90288e3e79a38c9f
                        at src/lib.rs:34
  11:     0x55e178a67c0c - _<std..panic..AssertUnwindSafe<F> as std..ops..FnOnce<()>>::call_once::ha07be642ca4bf6be
                        at ../src/libstd/panic.rs:284
  12:     0x55e178a67b54 - mioco::src::coroutine::Coroutine::spawn::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h8e8ee5d6ca3d2346
  13:     0x55e178a67977 - std::panic::catch_unwind::_$u7b$$u7b$closure$u7d$$u7d$::hb67bb79ad1a6b15a
  14:     0x55e178a6794d - std::sys_common::unwind::try::try_fn::h4379127dc35adf38
                        at ../src/libstd/sys/common/unwind/mod.rs:127
  15:     0x55e178ad9ebb - __rust_try
  16:     0x55e178ad9e4d - std::sys_common::unwind::inner_try::h47a4d9cd4a369dcd
  17:     0x55e178a678fd - std::sys_common::unwind::try::hc78290035883eba5
                        at ../src/libstd/sys/common/unwind/mod.rs:123
  18:     0x55e178a67760 - std::panic::catch_unwind::hf07e3a34327ea4ec
                        at ../src/libstd/panic.rs:389
  19:     0x55e178a66760 - mioco::src::coroutine::Coroutine::spawn::_$u7b$$u7b$closure$u7d$$u7d$::hf14494481f98e655
  20:     0x55e178a6ff94 - _<mioco..src..thunk..Thunk<'a, (), R>>::new::_$u7b$$u7b$closure$u7d$$u7d$::hec730d21675d49fe
  21:     0x55e178a6fefc - _<F as mioco..src..thunk..Invoke<A, R>>::invoke::haa5ca6339ae528e6
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/thunk.rs:47
  22:     0x55e178ac2d12 - _<src..thunk..Thunk<'a, A, R>>::invoke::h2660317a668d81e2
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/thunk.rs:33
  23:     0x55e178ac24c5 - mioco::src::coroutine::Coroutine::spawn::coroutine_context_start_fn::hb2ad76ddad0055cd
                        at /home/sapikachu/.multirust/toolchains/nightly/cargo/git/checkouts/mioco-85c98dc94171ee94/master/src/coroutine.rs:223
  24:     0x55e178acbc5a - <unknown>
                        at src/asm/make_x86_64_sysv_elf_gas.S:51
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Any', ../src/libcore/result.rs:785
stack backtrace:
   1:     0x55e178adace0 - std::sys::backtrace::tracing::imp::write::h9fb600083204ae7f
   2:     0x55e178add94b - std::panicking::default_hook::_$u7b$$u7b$closure$u7d$$u7d$::hca543c34f11229ac
   3:     0x55e178add5d3 - std::panicking::default_hook::hc2c969e7453d080c
   4:     0x55e178ad461f - std::sys_common::unwind::begin_unwind_inner::h30e12d15ce2b2e25
   5:     0x55e178ad51c8 - std::sys_common::unwind::begin_unwind_fmt::hb2de8a9968d38523
   6:     0x55e178ad9f31 - rust_begin_unwind
   7:     0x55e178b12d4f - core::panicking::panic_fmt::h257ceb0aa351d801
   8:     0x55e178a431d0 - core::result::unwrap_failed::h2cc106e731b24162
                        at ../src/libcore/macros.rs:29
   9:     0x55e178a43000 - _<std..result..Result<T, E>>::unwrap::hc95910105c6ee0ce
                        at ../src/libcore/result.rs:723
  10:     0x55e178a42f89 - mioco_test::main::hcead9942d5c0eb59
                        at src/lib.rs:8
  11:     0x55e178add214 - std::sys_common::unwind::try::try_fn::h04c0c89e4add6cc4
  12:     0x55e178ad9ebb - __rust_try
  13:     0x55e178adcc5b - std::rt::lang_start::h61f4934e780b4dfc
  14:     0x55e178aa71b9 - main
  15:     0x7f6882d91a3f - __libc_start_main
  16:     0x55e178a42e68 - _start
  17:                0x0 - <unknown>
dpc commented 8 years ago

Will look into that on evening. Thanks for reporting!

dpc commented 8 years ago

I have troubles reliably reproducing, since it's very timing dependent. But looking into the issue, it's just the panicking debug_asserts are not really holding at all times, so I removed them. The issue should be fixed in the master.

SAPikachu commented 8 years ago

Weird, it happens on every run on my system. Anyways thanks for the fix!

dpc commented 8 years ago

It was on my too if I used it exactly as is in your example. But when I wanted to add it to test suite and remove the println so they don't pollute the output, the problem was gone.