teloxide: thread 'main' has overflowed its stack

I tried this code: https://github.com/teloxide/teloxide/blob/master/crates/teloxide/examples/purchase.rs I expected to see this happen: https://github.com/teloxide/teloxide/blob/7b2de9ad39f3a0841f9058d21389805e4880724a/crates/teloxide/examples/purchase.rs#L11C1-L12C65

Instead, this happened:

Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
     Running `target\debug\untitled.exe untitled`

thread 'main' has overflowed its stack
error: process didn't exit successfully: `target\debug\untitled.exe untitled` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)

Meta

  • teloxide version: 0.13.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 35 (6 by maintainers)

Most upvoted comments

After some time, pain of windows development and lack of success i gathered that much:

  1. It is not a memory leak (it’s rust), all the memory actually gets used, and increasing the stack size actually fixes the overflow
  2. For some reason putting it all in a thread (without even specifying stack size) fixes the problem and lowers the needed stack size to 1.6Mb (below the default 2Mb)
  3. It is a problem with deserialization, my debuggers went to this update Message deserialization, which then pointed to this deserialize_map function (and then debuggers just gave plain assembly)
  4. I have absolutely no idea why it is windows specific. Default stack size is the same on windows and linux, maybe some windows api stuff, or maybe just windows being windows

Possible solutions:

1. Add

[profile.dev]
opt-level = 1

to Cargo.toml, it lowers the needed stack size to just 400Kb, but it increases the testing compilation time x2 (users won’t notice, they need to compile teloxide only once, but development will get slower). It is a solution that will fix all future stack problems, and a lot of projects do use it

2. Find some rust flag that doesn’t increase the compile time too much, but reduces the stack usage

(if someone knows it - please tell, i couldn’t find it at all)

3. Increase the stack size to 4Mb or 8Mb inside of teloxide (if even possible), maybe make windows-specific or debug only.

This solution has no large downsides, but I don’t know if it is good to force the stack size to be bigger by default, especially if someone wants to increase the stack size even more by themselves for their specific application

4. Find a way to optimize stack usage in teloxide?..

Not sure if possible

5. Try to find out why deserialization into Message itself causes so much trouble

Because deserializing into value like this in the update Message deserialization:

let val: Value = map.next_value::<serde_json::Value>().unwrap();

Works fine, but if i add

let m: Message = serde_json::from_str(&serde_json::to_string(&val).unwrap()).unwrap();

It causes an overflow

@syrtcevvi and @Hirrolot, what do you think? I’m completely lost on this, and this issue seems extremely important for windows devs, it is extremely easy to come across (just lauching (!!!) even the throw_dice (!!!) example exactly like said in the README does the trick, which is not good), and i think this needs to be addressed ASAP in the next release

If something needs to be tested - tell me, i’ll launch it on my machine

There are 2 problems, both of them are in serde One for incoming Message update deserialization (the original issue), and another is… Im not sure if its serialization or deserialization, but it’s also serde (because without deriving Serialize and Deserialize on State the trait bounds are not satisfied) I think its the same basic type of error, and finding a way to fix this more recreatable issue will probably give a way to fix the original issue

Actually, it has nothing to do with teloxide, but actually with tokio runtime, iirc. It’s possible to create the runtime with increased stack-size for worker-threads (you need to get rid of #[tokio::main] macro and create an instance of the runtime by yourself)

Well, we can’t expect users to do that just to launch cargo run on their bots

What is the best solution here in your opinion?

  1. Increase the stack size to 4Mb or 8Mb inside of teloxide (if even possible), maybe make windows-specific or debug only.

Actually, it has nothing to do with teloxide, but actually with tokio runtime, iirc. It’s possible to create the runtime with increased stack-size for worker-threads (you need to get rid of #[tokio::main] macro and create an instance of the runtime by yourself)

https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.thread_stack_size

P.s. --release flag turns on all possible compiler optimizations, it increases the compile time and the performance of the resulting program

P.s.s memory leaks in rust still possible due to the use of the old stuff, allocators, libs (c libs) (for instance, axum issues (or issued?) a memory leak)