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

[diagnostics_channel] tracingChannel.traceCallback incorrect types #50996

Closed
Semigradsky opened this issue Dec 1, 2023 · 4 comments · Fixed by #51068
Closed

[diagnostics_channel] tracingChannel.traceCallback incorrect types #50996

Semigradsky opened this issue Dec 1, 2023 · 4 comments · Fixed by #51068
Labels
diagnostics_channel Issues and PRs related to diagnostics channel doc Issues and PRs related to the documentations.

Comments

@Semigradsky
Copy link
Contributor

Affected URL(s)

https://nodejs.org/docs/latest-v18.x/api/diagnostics_channel.html#tracingchanneltracecallbackfn-position-context-thisarg-args

Description of the problem

tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]]) - almost all args are marked as optional but it can't be called only with the first argument.

const callback = ArrayPrototypeAt(args, position);
validateFunction(callback, 'callback');

^ callback is required, so we need to call it like this:

channels.traceCallback(
	function (callback) {
		// Do something
		callback(null, 'result');
	},
	undefined, // position - `-1` by default
	undefined, // context - `{}` by default
	undefined, // thisArg - any value
	callback,
)

So all arguments are required but some can be undefined.

@Semigradsky Semigradsky added the doc Issues and PRs related to the documentations. label Dec 1, 2023
@Flarna Flarna added the diagnostics_channel Issues and PRs related to diagnostics channel label Dec 5, 2023
@Flarna
Copy link
Member

Flarna commented Dec 5, 2023

Correct. As tracingChannel.traceCallback() is built for functions receiving at least a callback as argument it's clear that user will provide at least this callback. The defaults for position, context and thisArg aren't that helpful.

For cases where callback is actually optional in the traced function traceCallback() can't be used as of now.

Seems implementation is a bit inconsistent in this regard. While a check is done that callback is actually a function it's optional inside wrappedCallback.

@Qard I think we should relax the typeof check to at least allow null/undefined. This still results in mostly useless defaults for position, context and thisArg but at least the doc is correct again.

@Semigradsky
Copy link
Contributor Author

By doc I guess that I can call like

channels.traceCallback(
	function (callback) {
		// Do something
		callback(null, 'result');
	},
	callback,
)

but I can't 😸

@Qard
Copy link
Member

Qard commented Dec 5, 2023

I think I would prefer to adjust the docs to make the other args required as it may be a bit unexpected for a missing callback to be allowed when the function being wrapped potentially would have crashed if it didn't receive a callback. The traceCallback function should probably be similarly modified to only do the wrap when a callback is actually found and let the function being called fail on its own if the callback is not available.

A bit hard to decide. I'm not sure there's a clearly "correct" way to handle this generically. 🤔

@Flarna
Copy link
Member

Flarna commented Dec 6, 2023

created #51068

nodejs-github-bot pushed a commit that referenced this issue Dec 8, 2023
tracingChannel.traceCallback() requires a callback otherwise it throws
and invalid argument error. As a result arguments are not optional.

Correct the documentation to reflect that arguments are not optional.

Besides that correct description regarding signaling of errors.

Remove an unneeded null check in wrappedCallback() which can't happen
because it's validated that callback is of type function.

PR-URL: #51068
Fixes: #50996
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
RafaelGSS pushed a commit that referenced this issue Dec 15, 2023
tracingChannel.traceCallback() requires a callback otherwise it throws
and invalid argument error. As a result arguments are not optional.

Correct the documentation to reflect that arguments are not optional.

Besides that correct description regarding signaling of errors.

Remove an unneeded null check in wrappedCallback() which can't happen
because it's validated that callback is of type function.

PR-URL: #51068
Fixes: #50996
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
richardlau pushed a commit that referenced this issue Mar 25, 2024
tracingChannel.traceCallback() requires a callback otherwise it throws
and invalid argument error. As a result arguments are not optional.

Correct the documentation to reflect that arguments are not optional.

Besides that correct description regarding signaling of errors.

Remove an unneeded null check in wrappedCallback() which can't happen
because it's validated that callback is of type function.

PR-URL: #51068
Fixes: #50996
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostics_channel Issues and PRs related to diagnostics channel doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants