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

src: fix implementation of AsyncProgressWorker::Signal #1216

Closed

Conversation

KevinEady
Copy link
Contributor

Fix implementation of AsyncProgressWorker::Signal and AsyncProgressQueueWorker::Signal

Fixes: #1095
Fixes: #1081

Comment on lines 81 to 91
{
std::unique_lock<std::mutex> lock(_cvm);
// Testing a nullptr send is acceptable.
progress.Send(nullptr, 0);
_cv.wait(lock);
}
{
std::unique_lock<std::mutex> lock(_cvm);
progress.Signal();
_cv.wait(lock);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use the cv wait overload that has a condition function

@mhdawson
Copy link
Member

@KevinEady could you add a short explanation of how using SendProgress instead of Signal resolves the problem? I think I know but not 100% sure.

@legendecas would be good if you reviewed as well since it seems like you were in the loop on the discussions of the original problem.

@KevinEady
Copy link
Contributor Author

Hi @mhdawson ,

The AsyncProgress[Queue]Worker::Signal() method is supposed to call the OnProgress worker with nullptr for data and 0 for count. The main change is not using NonBlockingCall() but instead using SendProgress_().

I went about this PR by looking at the existing test we have:

// Testing a nullptr send is acceptable.
progress.Send(nullptr, 0);

Since sending nullptr with 0 data is allowed, I wrote Signal() to behave in this manner, by basically making progress.Signal() behave as progress.Send(nullptr, 0), which means using the SendProgress_() method.

Hope this clarifies!

Thanks, Kevin

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM

@mhdawson
Copy link
Member

@KevinEady many thanks for the explanation

Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

Sorry for the delay.

AsyncProgressWorker::Signal should not invalidate sent progress not handled, similar to https://github.com/nodejs/nan/blob/main/nan.h#L2088. Would you mind adding a test case for this?

napi-inl.h Outdated
@@ -6032,12 +6032,12 @@ inline void AsyncProgressWorker<T>::SendProgress_(const T* data, size_t count) {

template <class T>
inline void AsyncProgressWorker<T>::Signal() const {
this->NonBlockingCall(static_cast<T*>(nullptr));
this->SendProgress_(static_cast<T*>(nullptr), 0);
Copy link
Member

Choose a reason for hiding this comment

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

This will override saved progress and make unexpected side effects. We should save a bit of the signal and proceed with null data at https://github.com/nodejs/node-addon-api/blob/main/napi-inl.h#L5998 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @legendecas , please take a look at 1f2cd71. Is this what you are talking about with 'save a bit of the signal'?

I also added a test case were an OnProgress and Signal are performed in sequence, and checks that the progress data is present.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, this looks good!

@KevinEady
Copy link
Contributor Author

Another thing @legendecas , i did not make any changes to AsyncProgressQueueWorker regarding a state member like 1f2cd71. Since the queue variety of the progress worker stores all progress updates, I don't think it is necessary to update the queue worker. Does this sound right?

Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

LGTM. I think it would be great to add a caveat on https://github.com/nodejs/node-addon-api/blob/main/doc/async_worker_variants.md#onprogress about the nullability of the data parameter if the method Signal is used.

mhdawson pushed a commit that referenced this pull request Dec 2, 2022
PR-URL: #1216
Reviewed-By: Michael Dawson <midawson@redhat.com
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
@mhdawson
Copy link
Member

mhdawson commented Dec 2, 2022

Landed as edf630c

@mhdawson mhdawson closed this Dec 2, 2022
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
PR-URL: nodejs/node-addon-api#1216
Reviewed-By: Michael Dawson <midawson@redhat.com
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants