Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format paniced #1363

Closed
fengyangsi opened this issue Nov 21, 2023 · 11 comments
Closed

format paniced #1363

fengyangsi opened this issue Nov 21, 2023 · 11 comments

Comments

@fengyangsi
Copy link

fengyangsi commented Nov 21, 2023

I use chrono to translate timestamp to ISO format string in wasm program. And panic when call format function.

There was code

use worker::*;
use chrono::NaiveDateTime;

#[event(fetch)]
async fn main(req: Request, env: Env, ctx: Context) -> Result<Response> {
    panic::set_hook(Box::new(|_| {
        worker::console_error!("panic!");
    }));
    // this line didn't panic
    let now = NaiveDateTime::from_timestamp_millis(Date::now().as_millis() as i64).unwrap();
    // this line panic - format function
    Response::ok(now.format("%Y-%m-%dT%H:%M:%S%.f%:z").to_string())
}
    //panic
    now.format("%Y-%m-%dT%H:%M:%S%.f%:z")
    //also panic
    now.format("%+")

Cargo.toml

# https://github.com/rustwasm/wasm-pack/issues/1247
[package.metadata.wasm-pack.profile.release]
wasm-opt = false

[lib]
crate-type = ["cdylib"]

[dependencies]
chrono = "0.4.31"
worker = "0.0.18"

[profile.release]
lto = true
strip = true
codegen-units = 1

wrangler.toml

main = "build/worker/shim.mjs"
compatibility_date = "2023-11-20"

[build]
command = "worker-build"
#also panic when command = "worker-build --release"
@djc
Copy link
Member

djc commented Nov 21, 2023

What is the panic message?

@fengyangsi
Copy link
Author

What is the panic message?

panic occurred: panicked at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/alloc/src/string.rs:2460:14:
a Display implementation returned an error unexpectedly: Error
panic occurred: panicked at library/std/src/panicking.rs:135:9:
cannot modify the panic hook from a panicking thread

there are code

use worker::*;
use chrono::NaiveDateTime;
use std::panic;
#[event(fetch)]
async fn main(req: Request, env: Env, ctx: Context) -> Result<Response> {
    panic::set_hook(Box::new(|panic_info| {
        console_error!("panic occurred: {panic_info}");
    }));
    let now = NaiveDateTime::from_timestamp_millis(Date::now().as_millis() as i64).unwrap();
    Response::ok(now.format("%Y-%m-%dT%H:%M:%S%.f%:z").to_string())
}

@djc
Copy link
Member

djc commented Nov 22, 2023

I'd like a full back trace, please -- I can't easily reproduce a WASM environment.

@fengyangsi
Copy link
Author

I'd like a full back trace, please -- I can't easily reproduce a WASM environment.

Sorry but how to get full back trace?

I call panic::set_hook() and console panic_info, that's all it return.

@djc
Copy link
Member

djc commented Nov 24, 2023

Usually with a RUST_BACKTRACE=1 environment variable, not sure how that works on WASM. Or maybe just get a clone of chrono and start wiring in some println!()s to figure out what code exactly is triggering the panic.

@davehorner
Copy link

davehorner commented Nov 24, 2023

I tried creating a test.

#[cfg(test)]
#[test]
/// now.format should not panic
fn assert_panic_nowformat() {
    let now = NaiveDateTime::from_timestamp_millis(NaiveDate::now().as_millis() as i64).unwrap();
    let ret = now.format("%Y-%m-%dT%H:%M:%S%.f%:z").to_string();
}
error[E0599]: no function or associated item named `now` found for struct `Date` in the current scope
  --> tests\panics.rs:25:58
   |
25 |     let now = NaiveDateTime::from_timestamp_millis(Date::now().as_millis() as i64).unwrap();
   |                                                          ^^^ function or associated item not found in `Date<_>`
warning: use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Date::now() doesn't exist for me on windows, I haven't setup a WASM build yet. I tried using NaiveDate::now() that also is not there. I can't compile the code provided at least on windows anyway.

@fengyangsi
Copy link
Author

use chrono::NaiveDateTime;
fn main() {
    let t = NaiveDateTime::from_timestamp_millis(1662921288000).unwrap();
    let f = t.format("%+").to_string();
    println!("{f}");
}
RUST_BACKTRACE=full cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello`
thread 'main' panicked at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/alloc/src/string.rs:2460:14:
a Display implementation returned an error unexpectedly: Error
stack backtrace:
   0:     0x5606f5b3f66c - std::backtrace_rs::backtrace::libunwind::trace::h67a838aed1f4d6ec
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x5606f5b3f66c - std::backtrace_rs::backtrace::trace_unsynchronized::h1d1786bb1962baf8
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x5606f5b3f66c - std::sys_common::backtrace::_print_fmt::h5a0b1f807a002d23
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x5606f5b3f66c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf84ab6ad0b91784c
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x5606f5b5ef1c - core::fmt::rt::Argument::fmt::h28f463bd1fdabed5
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/fmt/rt.rs:138:9
   5:     0x5606f5b5ef1c - core::fmt::write::ha37c23b175e921b3
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/fmt/mod.rs:1114:21
   6:     0x5606f5b3d42e - std::io::Write::write_fmt::haa1b000741bcbbe1
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/io/mod.rs:1763:15
   7:     0x5606f5b3f454 - std::sys_common::backtrace::_print::h1ff1030b04dfb157
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x5606f5b3f454 - std::sys_common::backtrace::print::hb982056c6f29541c
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x5606f5b409d3 - std::panicking::default_hook::{{closure}}::h11f92f82c62fbd68
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:272:22
  10:     0x5606f5b406f4 - std::panicking::default_hook::hb8810fe276772c66
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:292:9
  11:     0x5606f5b40f55 - std::panicking::rust_panic_with_hook::hd2f0efd2fec86cb0
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:731:13
  12:     0x5606f5b40e51 - std::panicking::begin_panic_handler::{{closure}}::h3651b7fc4f61d784
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:609:13
  13:     0x5606f5b3fb96 - std::sys_common::backtrace::__rust_end_short_backtrace::hbc468e4b98c7ae04
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/sys_common/backtrace.rs:170:18
  14:     0x5606f5b40ba2 - rust_begin_unwind
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
  15:     0x5606f5b0d185 - core::panicking::panic_fmt::h979245e2fdb2fabd
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14
  16:     0x5606f5b0d5e3 - core::result::unwrap_failed::h8c4b86241881fbbb
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1652:5
  17:     0x5606f5b0d9fe - core::result::Result<T,E>::expect::h1bf588d5c1a14540
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1034:23
  18:     0x5606f5b0ead2 - <T as alloc::string::ToString>::to_string::hb8d5d970ca5961bd
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/alloc/src/string.rs:2459:9
  19:     0x5606f5b0e54e - hello::main::hf9221dc4589f196b
                               at /home/yang/Codes/hello/src/main.rs:4:13
  20:     0x5606f5b0dabb - core::ops::function::FnOnce::call_once::h4f94d5cc56dccc29
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
  21:     0x5606f5b0e15e - std::sys_common::backtrace::__rust_begin_short_backtrace::h810b7239c1ae5331
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/sys_common/backtrace.rs:154:18
  22:     0x5606f5b0dbe1 - std::rt::lang_start::{{closure}}::h73936dde8a6a720e
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/rt.rs:166:18
  23:     0x5606f5b3addb - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::hf9057cfaeeb252e2
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:284:13
  24:     0x5606f5b3addb - std::panicking::try::do_call::h629e203a624883e4
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:504:40
  25:     0x5606f5b3addb - std::panicking::try::h7b61614724d6a4f1
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:468:19
  26:     0x5606f5b3addb - std::panic::catch_unwind::h354ac1c0268491d8
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panic.rs:142:14
  27:     0x5606f5b3addb - std::rt::lang_start_internal::{{closure}}::h919fee3c5ba8f617
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/rt.rs:148:48
  28:     0x5606f5b3addb - std::panicking::try::do_call::h54583f67455bff32
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:504:40
  29:     0x5606f5b3addb - std::panicking::try::hb0e12c4e01d39dc2
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:468:19
  30:     0x5606f5b3addb - std::panic::catch_unwind::h367b6339e3ca9a3b
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panic.rs:142:14
  31:     0x5606f5b3addb - std::rt::lang_start_internal::ha5ce8533eaa0fda8
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/rt.rs:148:20
  32:     0x5606f5b0dbba - std::rt::lang_start::h82ede418019d1799
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/rt.rs:165:17
  33:     0x5606f5b0e69e - main
  34:     0x7fe2d9bf424d - __libc_start_main
  35:     0x5606f5b0d8ba - _start
                               at /home/abuild/rpmbuild/BUILD/glibc-2.31/csu/../sysdeps/x86_64/start.S:120
  36:                0x0 - <unknown>

@djc
Copy link
Member

djc commented Nov 27, 2023

Well, that trace has no chrono stack frames, so it doesn't help much. :(

@pitdicker
Copy link
Collaborator

Can you test it with Response::ok(now.format("%Y-%m-%dT%H:%M:%S%.f").to_string())?

I think the issue is %:z: NaiveDateTime does not have an offset that you can print.

@fengyangsi
Copy link
Author

Can you test it with Response::ok(now.format("%Y-%m-%dT%H:%M:%S%.f").to_string())?

I think the issue is %:z: NaiveDateTime does not have an offset that you can print.

it works, didn't panic

@pitdicker
Copy link
Collaborator

Thank you for testing. That would make this issue a duplicate of #1096, and #1127 has the plan to return an error instead of panicking.

In your case you just want to convert a Unix timestamp to RFC 3339 format? Than something like this should do the trick:

use chrono::{Utc, TimeZone};
let response_str = Utc.timestamp_millis_opt(1662921288000).unwrap().to_rfc3339();

@djc djc closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants