Skip to content

Wrapped objects don't get dropped? #4

Closed

Description

@Frando

When wrapping a native struct in a js object, the struct never seems to be dropped. No matter if the node process crashed or exits gracefully, a impl Dropon a struct that was wrapped never gets called.

I can provide a test case - would be useful though if before the changes from @cztomsik could be pulled in because I worked upon these.

Activity

  1. cztomsik commented on Feb 20, 2019

    @cztomsik
  2. Frando commented on Feb 20, 2019

    @Frando
    ContributorAuthor

    Thanks for the reply, and interesting approach.

    What's the advantage - the codegen from typescript interfaces?

  3. cztomsik commented on Feb 20, 2019

    @cztomsik

    shallow api (just send(Msg) where Msg is like Union of all possible message types) has some benefits:

    • less binding code (routing is just ordinary rust, it's not specific to node)
    • theoretically, we can receive messages from anything (ipc, network), not just ffi
    • it's easy to do cross-cutting thing in one place (logging, profiling)
    • we wanted to batch multiple calls to one (avoid js <-> rust crossing)

    it could be done with napi-rs too, but we would need to introduce arraybuffers and in overall node-ffi felt like better choice (it works with callbacks too which was my original motivation for my own fork of napi-rs)

  4. colbyn commented on Oct 13, 2019

    @colbyn

    @Frando Without any further information. If your finalize_cb callback is setup correctly, it’s probably due to some GC optimization. Since when the process exits everything gets freed already and presumably faster by the OS.

  5. colbyn commented on Oct 13, 2019

    @colbyn

    If it helps, personally I’ve been messing around with an image processing library, although it doesn’t use this specific library. Heres a snippet using the NAPI extern feature:

    pub fn to_extern<T>(env: napi_env, data: T) -> napi_value {
        pub unsafe extern "C" fn finalize_value<T>(
            env: napi_env,
            finalize_data: *mut ::std::os::raw::c_void,
            finalize_hint: *mut ::std::os::raw::c_void,
        ) {
            println!("finalize called!");
            assert!(!finalize_data.is_null());
            let data = Box::from_raw(finalize_data as *mut T);
            std::mem::drop(data);
            println!("finalize done");
        }
        unsafe {
            let data = Box::new(data);
            let data = Box::into_raw(data);
            let mut output = std::mem::uninitialized();
            let status = napi_create_external(
                env,
                data as *mut libc::c_void,
                Some(finalize_value::<T>),
                std::ptr::null_mut(),
                &mut output,
            );
            assert!(status == napi_status_napi_ok);
            assert!(!output.is_null());
            output
        }
    }
    

    To test that finalize_value runs correctly, you can invoke node with node --expose_gc …, then in your JS code manually trigger the GC with something like this:

    let img = image.open(x);
    img = null;
    global.gc();
    

    This is the best I’ve figured out so far…

    One thing that’s been on my mind. From the docs, I’ve noticed that NAPI exposes a function called napi_adjust_external_memory that quote “will trigger global garbage collections more often than it would otherwise”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions