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

fix(napi): Implement napi_threadsafe_function ref and unref #17304

Merged
merged 6 commits into from
Jan 12, 2023

Conversation

littledivy
Copy link
Member

Closes #17288

@littledivy
Copy link
Member Author

littledivy commented Jan 8, 2023

AFAIU ref/unref do the same thing as release/aquire functions but the later also consumes/takes ownership of the tsfn handle.

https://nodejs.org/api/n-api.html#napi_unref_threadsafe_function

@littledivy littledivy marked this pull request as ready for review January 8, 2023 05:11
Comment on lines 39 to 50
self
.tsfn_sender
.unbounded_send(ThreadSafeFunctionStatus::Alive)
.map_err(|_| Error::GenericFailure)?;
Ok(())
}

pub fn unref(&mut self) -> Result {
self
.tsfn_sender
.unbounded_send(ThreadSafeFunctionStatus::Dead)
.map_err(|_| Error::GenericFailure)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is brittle and not idempotent - calling unref() multiple times will make NapiState.active_threadsafe_functions go negative. I was looking into fixing this and I believe the most bulletproof solution would either be implementing something similar to what @aapoalas did for FFI functions, or make TsFn have ID and store a hashmap of TsFn mappings in NapiState that keeps track of reference counts. WDYT?

@SevInf
Copy link

SevInf commented Jan 10, 2023

AFAIU ref/unref do the same thing as release/aquire functions but the later also consumes/takes ownership of the tsfn handle.

In Node ref/unref is completely unrelated to acquire/release API — they do not affect ThreadsafeFunction reference counter at all. Source for unref vs release.

@bartlomieju
Copy link
Member

AFAIU ref/unref do the same thing as release/aquire functions but the later also consumes/takes ownership of the tsfn handle.

In Node ref/unref is completely unrelated to acquire/release API — they do not affect ThreadsafeFunction reference counter at all. Source for unref vs release.

Thanks for pointing this out, we discussed this offline earlier today and decided that this integration needs to be mostly rewritten. I'll look into that.

@bartlomieju bartlomieju changed the title feat(cli/napi): Implement napi_threadsafe_function ref and unref fix(napi): Implement napi_threadsafe_function ref and unref Jan 11, 2023
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bartlomieju bartlomieju merged commit dd2829b into denoland:main Jan 12, 2023
@bartlomieju bartlomieju deleted the napi_tsfn_ref branch January 12, 2023 12:47
dsherret pushed a commit that referenced this pull request Jan 13, 2023
dsherret pushed a commit that referenced this pull request Jan 13, 2023
MierenManz added a commit to MierenManz/oned that referenced this pull request Jan 20, 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

Successfully merging this pull request may close these issues.

hang after importing node-api module
3 participants