Skip to content

Share a trap I encountered. #278

@yyy33

Description

@yyy33

As you can see, I've set the setup method to fnonce here, which is a major issue.
Your plugin might run normally, or it could cause nvim to crash unexpectedly—it all depends on whether setup gets called a second time. I'm not sure why, but my plugin compiled and worked fine initially. After some time, lazyvim started repeatedly calling my setup method for unknown reasons, causing nvim to crash constantly with hard-to-understand error messages. I spent an entire night troubleshooting this issue, and solving it was pure luck—I accidentally spotted the from_fnonce method.

fn translate() -> Dictionary {
    // set_panic_hook();
    let worker = Worker::default();

    let setup: Object = Function::from_fn_once({
        let worker = worker.clone();
        move |opts: Opts| {
            // println!("33333333333333333333");
            // let _ = ManuallyDrop::new(opts);
            // println!("44444444444444444444");
            // setup(opts, worker).echo_err();
        }
    })
    .into();

    let switch_to_target: Object = Function::from_fn({
        let worker = worker.clone();
        move |()| {
            let buf = api::get_current_buf();
            worker.try_send(UserEvent::SwitchToTarget(buf.into()));
        }
    })
    .into();

    let switch_to_source: Object = Function::from_fn({
        let worker = worker.clone();
        move |()| {
            let buf = api::get_current_buf();
            worker.try_send(UserEvent::SwitchToSource(buf.into()));
        }
    })
    .into();

    let set_auto_translate: Object = Function::from_fn({
        let worker = worker.clone();
        move |(auto, buffer): (bool, nvim_oxi::api::Buffer)| {
            worker.try_send(UserEvent::SetAutoTranslate {
                auto,
                buffer: buffer.into(),
            });
        }
    })
    .into();

    let set_translate_mode: Object = Function::from_fn({
        let worker = worker.clone();
        move |(mode, buffer): (EngineMode, nvim_oxi::api::Buffer)| {
            worker.try_send(UserEvent::SetTranslateMode {
                mode,
                buffer: buffer.into(),
            });
        }
    })
    .into();

    Dictionary::from_iter([
        ("setup", setup),
        ("switch_source", switch_to_source),
        ("switch_target", switch_to_target),
        ("set_auto_translate", set_auto_translate),
        ("set_translate_mode", set_translate_mode),
    ])
}
]11;?�
thread '<unnamed>' (78684) panicked at library/core/src/panicking.rs:225:5:
panic in a function that cannot unwind
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_nounwind_fmt
   2: core::panicking::panic_nounwind
   3: core::panicking::panic_cannot_unwind
   4: nvim_oxi_luajit::function::store::c_fun
   5: <unknown>
   6: lua_pcall
   7: nlua_pcall
   8: nlua_exec_file
   9: <unknown>
  10: do_source
  11: <unknown>
  12: main
  13: <unknown>
  14: __libc_start_main
  15: _start
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions