Skip to content

Comments

napi_async_work fixes#18825

Merged
Jarred-Sumner merged 2 commits intomainfrom
dylan/napi-async-work-missing-null-checks
Apr 7, 2025
Merged

napi_async_work fixes#18825
Jarred-Sumner merged 2 commits intomainfrom
dylan/napi-async-work-missing-null-checks

Conversation

@dylan-conway
Copy link
Member

What does this PR do?

Fixes four bugs:

  • null check for execute callback (return napi_invalid_arg if null).
  • null check complete callback after work is done (do nothing if null).
  • fixes napi_cancel_async_work. Returns napi_cancelled through complete.
  • does not free napi_async_work through any function other than napi_delete_async_work.

How did you verify your code works?

Added a test for null execute, null complete, and receiving napi_cancelled after napi_cancel_async_work

@robobun
Copy link
Collaborator

robobun commented Apr 7, 2025

Updated 4:17 AM PT - Apr 7th, 2025

@dylan-conway, your commit ff08e62146ee462da9c6a8c47983e6314e3931b6 passed in Build #14557! 🎉


🧪   try this PR locally:

bunx bun-pr 18825

@Jarred-Sumner Jarred-Sumner merged commit 340ae94 into main Apr 7, 2025
70 checks passed
@Jarred-Sumner Jarred-Sumner deleted the dylan/napi-async-work-missing-null-checks branch April 7, 2025 12:20
Copy link
Contributor

@190n 190n left a comment

Choose a reason for hiding this comment

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

Some style feedback on the tests, but nothing major

Comment on lines +221 to +222
int32_t *data = new int32_t;
*data = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

doesn't seem necessary if execute_for_null_complete isn't using it

Copy link
Member Author

Choose a reason for hiding this comment

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

will cleanup in #18832

};

void execute_for_cancel(napi_env env, void *data) {
// nothing
Copy link
Contributor

Choose a reason for hiding this comment

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

this should never be called, right? we could have it print something or crash so the test can check if it was called

Copy link
Member Author

Choose a reason for hiding this comment

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

It won't be called, but it's needed because napi_create_async_work validates it and return napi_invalid_arg if it's null

Copy link
Contributor

Choose a reason for hiding this comment

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

I wasn't suggesting to pass null, I was suggesting to make the function print something so we can make sure it's not erroneously called

Copy link
Member Author

Choose a reason for hiding this comment

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

oops misread, yeah that's a good idea

Comment on lines +40 to +52
nativeTests.test_napi_async_work_execute_null_check = () => {
const res = nativeTests.create_async_work_with_null_execute();
if (res) {
console.log("success!");
} else {
console.log("failure!");
}
};

nativeTests.test_napi_async_work_complete_null_check = async () => {
nativeTests.create_async_work_with_null_complete();
await gcUntil(() => true);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

These two could probably just be called directly by napi.test.ts (and have the native code print out whether everything's okay) instead of having new code in module.js

Copy link
Member Author

Choose a reason for hiding this comment

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

true, will also clean this up in #18832

if (str) |ptr| {
if (NAPI_AUTO_LENGTH == length) {
break :brk bun.sliceTo(@as([*:0]const u8, @ptrCast(str)), 0);
break :brk bun.sliceTo(@as([*:0]const u8, @ptrCast(ptr)), 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

surprised this ptrCast (in the former code where the input was an optional) is legal

@190n
Copy link
Contributor

190n commented Apr 7, 2025

Jarred-Sumner pushed a commit that referenced this pull request Apr 7, 2025
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.

4 participants