pelikan-io / rustcommon

Common Rust library
Apache License 2.0
26 stars 13 forks source link

ratelimiter: thread '<unnamed>' panicked at 'attempt to subtract with overflow' #72

Closed pranavpadmasali closed 1 year ago

pranavpadmasali commented 1 year ago

When the ratelimiter is used with Rayon parallel iterator the execution panics with an error thread '<unnamed>' panicked at 'attempt to subtract with overflow', /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/ratelimit-0.7.0/src/lib.rs:266:23

Expected behavior

The Rayon thread and program should not panic for subtract with overflow and continue execution.

Actual behavior

The subtract overflow seems to occurs at this statement. The thread panics with this error.

Got 12 
Got 18 
Got 4 
thread '<unnamed>' panicked at 'attempt to subtract with overflow', /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/ratelimit-0.7.0/src/lib.rs:266:23
stack backtrace:
Got 2 
Got 6 
Got 5 
   0:        0x100c54786 - std::backtrace_rs::backtrace::libunwind::trace::h863be41b53c4f884
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:        0x100c54786 - std::backtrace_rs::backtrace::trace_unsynchronized::ha57069b30dcbe3f5
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:        0x100c54786 - std::sys_common::backtrace::_print_fmt::h7dbf0829c4d9fd22
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/sys_common/backtrace.rs:65:5
   3:        0x100c54786 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::he84dde8223670de4
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/sys_common/backtrace.rs:44:22
   4:        0x100c68fea - core::fmt::write::h0721d4325d1d86bb
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/fmt/mod.rs:1232:17
   5:        0x100c5209c - std::io::Write::write_fmt::hf4d04b4d333539fa
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/io/mod.rs:1684:15
   6:        0x100c5456a - std::sys_common::backtrace::_print::h15efcfdd13c9c026
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/sys_common/backtrace.rs:47:5
   7:        0x100c5456a - std::sys_common::backtrace::print::hed06273df3ccacbe
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/sys_common/backtrace.rs:34:9
   8:        0x100c55ca3 - std::panicking::default_hook::{{closure}}::he8c58e8d0abf03c5
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:271:22
   9:        0x100c559f8 - std::panicking::default_hook::h8320561bf55c0290
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:290:9
  10:        0x100c5628b - std::panicking::rust_panic_with_hook::h38dc40cfa47552ba
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:692:13
  11:        0x100c56163 - std::panicking::begin_panic_handler::{{closure}}::h727c67dc750b5cee
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:581:13
  12:        0x100c54bc9 - std::sys_common::backtrace::__rust_end_short_backtrace::h6848121e0bfe63be
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/sys_common/backtrace.rs:150:18
  13:        0x100c55e6d - rust_begin_unwind
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:579:5
  14:        0x100c75ab3 - core::panicking::panic_fmt::h690b01655a033c26
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panicking.rs:64:14
  15:        0x100c75b47 - core::panicking::panic::h5dd2e5155f42ce6e
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panicking.rs:114:5
  16:        0x100c191bb - ratelimit::Ratelimiter::try_wait::hb418d7565e28f84a
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/ratelimit-0.7.0/src/lib.rs:266:23
  17:        0x100b4bcbb - check::main::{{closure}}::{{closure}}::h65cefc89a4f15733
                               at /Users/pranavpadmasali/Projects/test/check/src/main.rs:18:36
  18:        0x100b4b73a - core::ops::function::impls::<impl core::ops::function::FnMut<A> for &F>::call_mut::hb082802ee3be1be2
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/function.rs:274:13
  19:        0x100b507da - core::iter::traits::iterator::Iterator::for_each::call::{{closure}}::h15383fe2091e793f
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/iter/traits/iterator.rs:849:29
  20:        0x100b4f8bd - core::iter::traits::iterator::Iterator::fold::h78002b3beb5b88ad
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/iter/traits/iterator.rs:2477:21
  21:        0x100b4f931 - core::iter::traits::iterator::Iterator::for_each::h572a09ce7e5b76a9
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/iter/traits/iterator.rs:852:9
  22:        0x100b5b612 - <rayon::iter::for_each::ForEachConsumer<F> as rayon::iter::plumbing::Folder<T>>::consume_iter::habeef7f29f2a5098
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/for_each.rs:55:9
  23:        0x100b4fb4a - rayon::iter::plumbing::Producer::fold_with::h28eb0767ff4347df
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:110:9
  24:        0x100b562c8 - rayon::iter::plumbing::bridge_producer_consumer::helper::had7f3fb0167377be
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:438:13
  25:        0x100b564ad - rayon::iter::plumbing::bridge_producer_consumer::helper::{{closure}}::hab977f91994e6bc4
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:418:21
  26:        0x100b554e4 - rayon_core::join::join_context::call_a::{{closure}}::hb360c037f0c211e1
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:124:17
  27:        0x100b4b3a2 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h174bba4eb965630b
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panic/unwind_safe.rs:271:9
  28:        0x100b559dc - std::panicking::try::do_call::h49859f0160d9235a
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:487:40
  29:        0x100b55ccd - ___rust_try
  30:        0x100b556f6 - std::panicking::try::h99d699244b9a31f1
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:451:19
  31:        0x100b4a749 - std::panic::catch_unwind::ha715480a7d40a25a
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panic.rs:140:14
  32:        0x100b4f7d0 - rayon_core::unwind::halt_unwinding::he39799be7575c13d
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/unwind.rs:17:5
  33:        0x100b55156 - rayon_core::join::join_context::{{closure}}::h807fbad48293b384
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:142:24
  34:        0x100b4db21 - rayon_core::registry::in_worker::he4ef3dca985f6a85
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:950:13
  35:        0x100b54f19 - rayon_core::join::join_context::hdc2fb33360863308
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:132:5
  36:        0x100b561d6 - rayon::iter::plumbing::bridge_producer_consumer::helper::had7f3fb0167377be
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:416:47
  37:        0x100b564ad - rayon::iter::plumbing::bridge_producer_consumer::helper::{{closure}}::hab977f91994e6bc4
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:418:21
  38:        0x100b554e4 - rayon_core::join::join_context::call_a::{{closure}}::hb360c037f0c211e1
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:124:17
  39:        0x100b4b3a2 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h174bba4eb965630b
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panic/unwind_safe.rs:271:9
  40:        0x100b559dc - std::panicking::try::do_call::h49859f0160d9235a
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:487:40
  41:        0x100b55ccd - ___rust_try
  42:        0x100b556f6 - std::panicking::try::h99d699244b9a31f1
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:451:19
  43:        0x100b4a749 - std::panic::catch_unwind::ha715480a7d40a25a
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panic.rs:140:14
  44:        0x100b4f7d0 - rayon_core::unwind::halt_unwinding::he39799be7575c13d
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/unwind.rs:17:5
  45:        0x100b55156 - rayon_core::join::join_context::{{closure}}::h807fbad48293b384
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:142:24
  46:        0x100b4db21 - rayon_core::registry::in_worker::he4ef3dca985f6a85
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:950:13
  47:        0x100b54f19 - rayon_core::join::join_context::hdc2fb33360863308
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:132:5
  48:        0x100b561d6 - rayon::iter::plumbing::bridge_producer_consumer::helper::had7f3fb0167377be
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:416:47
  49:        0x100b564ad - rayon::iter::plumbing::bridge_producer_consumer::helper::{{closure}}::hab977f91994e6bc4
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:418:21
  50:        0x100b554e4 - rayon_core::join::join_context::call_a::{{closure}}::hb360c037f0c211e1
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:124:17
  51:        0x100b4b3a2 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h174bba4eb965630b
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panic/unwind_safe.rs:271:9
  52:        0x100b559dc - std::panicking::try::do_call::h49859f0160d9235a
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:487:40
  53:        0x100b55ccd - ___rust_try
  54:        0x100b556f6 - std::panicking::try::h99d699244b9a31f1
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:451:19
  55:        0x100b4a749 - std::panic::catch_unwind::ha715480a7d40a25a
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panic.rs:140:14
  56:        0x100b4f7d0 - rayon_core::unwind::halt_unwinding::he39799be7575c13d
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/unwind.rs:17:5
  57:        0x100b55156 - rayon_core::join::join_context::{{closure}}::h807fbad48293b384
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:142:24
  58:        0x100b4db21 - rayon_core::registry::in_worker::he4ef3dca985f6a85
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:950:13
  59:        0x100b54f19 - rayon_core::join::join_context::hdc2fb33360863308
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:132:5
  60:        0x100b561d6 - rayon::iter::plumbing::bridge_producer_consumer::helper::had7f3fb0167377be
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:416:47
  61:        0x100b563f0 - rayon::iter::plumbing::bridge_producer_consumer::helper::{{closure}}::h77f3cec687d7d03a
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.8.0/src/iter/plumbing/mod.rs:427:21
  62:        0x100b555c9 - rayon_core::join::join_context::call_b::{{closure}}::he25b9af608dbed7e
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/join/mod.rs:129:25
  63:        0x100b4c307 - rayon_core::job::JobResult<T>::call::{{closure}}::h19c91e331e1bc5bc
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/job.rs:218:41
  64:        0x100b4b3d2 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h420589d97503eea1
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panic/unwind_safe.rs:271:9
  65:        0x100b55b1c - std::panicking::try::do_call::hf1d72fe7f63b2eb7
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:487:40
  66:        0x100b55ccd - ___rust_try
  67:        0x100b557a6 - std::panicking::try::ha8cda1fe9defb2bb
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:451:19
  68:        0x100b4a759 - std::panic::catch_unwind::hb957639e5ccf5a9a
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panic.rs:140:14
  69:        0x100b4f780 - rayon_core::unwind::halt_unwinding::hb441657cf7a74684
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/unwind.rs:17:5
  70:        0x100b4c278 - rayon_core::job::JobResult<T>::call::hea99445f989adda1
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/job.rs:218:15
  71:        0x100b4cdf4 - <rayon_core::job::StackJob<L,F,R> as rayon_core::job::Job>::execute::hf862ef431db56497
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/job.rs:120:32
  72:        0x100bf0f12 - rayon_core::job::JobRef::execute::h7da3311061489597
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/job.rs:64:9
  73:        0x100c0177f - rayon_core::registry::WorkerThread::execute::h2129a5b424508050
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:859:9
  74:        0x100c7109e - rayon_core::registry::WorkerThread::wait_until_cold::h261d38e3c61320ca
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:793:21
  75:        0x100c014bd - rayon_core::registry::WorkerThread::wait_until::h198cd1b50fdfe8dc
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:768:13
  76:        0x100c01603 - rayon_core::registry::WorkerThread::wait_until_out_of_work::h9828302a7862b07f
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:817:9
  77:        0x100c01ba2 - rayon_core::registry::main_loop::h9d21d6bc3ff08433
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:922:5
  78:        0x100bfecb9 - rayon_core::registry::ThreadBuilder::run::he98435407d1b47c7
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:52:18
  79:        0x100bff0d2 - <rayon_core::registry::DefaultSpawn as rayon_core::registry::ThreadSpawn>::spawn::{{closure}}::h4e1cd77da73186b0
                               at /Users/pranavpadmasali/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.12.0/src/registry.rs:97:20
  80:        0x100bf9ded - std::sys_common::backtrace::__rust_begin_short_backtrace::h8a971ea294c2e848
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/sys_common/backtrace.rs:134:18
  81:        0x100c073b2 - std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}::hab1e0e24bfa2457e
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/thread/mod.rs:560:17
  82:        0x100bf01a2 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h3f631a621e4b3e80
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panic/unwind_safe.rs:271:9
  83:        0x100bf2242 - std::panicking::try::do_call::h3b27ebb86e73c7e8
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:487:40
  84:        0x100bf2bbd - ___rust_try
  85:        0x100bf1f2c - std::panicking::try::h24693aa0035b749f
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:451:19
  86:        0x100c06639 - std::panic::catch_unwind::h7a44a865bfae8d25
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panic.rs:140:14
  87:        0x100c071e0 - std::thread::Builder::spawn_unchecked_::{{closure}}::h2f1f6e6eeb4f2a94
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/thread/mod.rs:559:30
  88:        0x100c022e1 - core::ops::function::FnOnce::call_once{{vtable.shim}}::hb73da6ebee8d540c
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/function.rs:250:5
  89:        0x100c58447 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hf3820a8c1b13028b
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/alloc/src/boxed.rs:1987:9
  90:        0x100c58447 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hc2e5a601c62c6604
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/alloc/src/boxed.rs:1987:9
  91:        0x100c58447 - std::sys::unix::thread::Thread::new::thread_start::hd752b82ee9981a13
                               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/sys/unix/thread.rs:108:17
  92:     0x7ff8011d91d3 - __pthread_start
Got 3 
Got 9

Steps to reproduce the behavior

Reproducer code

This is a minimal reproducer for the above error.

main.rs
use ratelimit::Ratelimiter;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use tokio::time::{Duration, sleep};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut vector_of_ints: Vec<i32> = Vec::new();
    for i in 1..=30 {
        vector_of_ints.push(i);
    }
    loop {
        let ratelimiter = Ratelimiter::builder(3, std::time::Duration::from_secs(1)).max_tokens(3)
            .initial_available(3)
            .build()
            .unwrap();

        vector_of_ints.clone().into_par_iter().for_each(|z| {
            while let Err(sleep) = ratelimiter.try_wait() {
                std::thread::sleep(sleep);
                continue;
            }
            // do some ratelimited action here
            // Use blocking call to call the asynchronous function
            tokio::runtime::Runtime::new().unwrap().block_on(async {
                multi_quote(z).await.expect("Error in multi quote");
            });
        });
    }
    Ok(())
}

pub async fn multi_quote(tokens: i32) -> Result<(), Box<(dyn std::error::Error + 'static)>>{
    print!("Got {} \n", tokens);
    let duration = Duration::from_millis(10);
    sleep(duration).await;
    Ok(())
}
cargo.toml dependencies
[dependencies]
ratelimit = "0.7.0"
tokio = { version = "1", features = ["full"] }
rayon = "1.7.0"

Executing with bash RUST_BACKTRACE=full cargo run The return code is

echo $?                                              
101
pranavpadmasali commented 1 year ago

I'm new to the Rust world. After reading a solution on StackOverflow that suggested using cargo run --release, the error seems to have been resolved. Now, I'm not sure if the above issue is still relevant or not.

brayniac commented 1 year ago

@pranavpadmasali - we should avoid panic even in debug mode. Let me take a look and see what needs to be fixed.

Thanks for the report!

brayniac commented 1 year ago

This is fixed by #73 - and is published to crates.io as version 0.7.1

Thanks again for the detailed report and the reproducer for this issue!