-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
bridge: Report PID for a spawn stream channel #21132
Conversation
For posterity, a cockpit.js API could look like this: --- pkg/lib/cockpit.js
+++ pkg/lib/cockpit.js
@@ -1447,9 +1447,12 @@ function factory() {
/* Callback that wants a stream response, see below */
const buffer = channel.buffer(null);
+ channel.addEventListener("ready", (_, message) => { dfd.promise.pid = message.pid });
+
channel.addEventListener("close", function(event, options) {
const data = buffer.squash();
spawn_debug("process closed:", JSON.stringify(options));
+ dfd.promise.pid = undefined;
if (data)
spawn_debug("process output:", data);
if (options.message !== undefined) But its awkward -- it slaps custom stuff on the cockpit promise, and we want to get rid of these. It's also difficult to test/use, as a cockpit.spawn() object neither has |
@allisonkarlitskaya pinging you as the pybridge is close to your heart. But please absolutely feel free to ignore this review request. |
Yeah, let's make a new API |
pkg/lib/cockpit.d.ts
Outdated
@@ -77,7 +77,7 @@ declare module 'cockpit' { | |||
(event: CustomEvent<Parameters<E>>, ...args: Parameters<E>) => void; | |||
|
|||
interface EventSource<EM extends EventMap> { | |||
addEventListener<E extends keyof EM>(event: E, listener: EventListener<EM[E]>): void; | |||
addEventListener<E extends keyof EM>(event: E, listener: EventListener<EM[E]>, options?: JsonObject): void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is wrong. Our addEventListener
implementation does not support the options
parameter. Calling it with one should be an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, TIL -- this is not the standard EventSource, but our custom pkg/lib/cockpit/_internal/event-mixin.js via pkg/lib/cockpit/_internal/channel.js. Eww! I took out that commit then.
@ashley-cui so, my example code above "silently" works, but is buggy and leaks event handlers. It needs to store the result of addEventListener(), and explicitly call removeEventListener() on them on close
. (but that's details which we can sort out in the PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It needs to store the result of addEventListener(), and explicitly call removeEventListener() on them on close.
Aren't event handlers removed by the GC when the channel object is collected? We don't remove event handlers from our channel objects explicitly in general.
If you want a guarantee that your handler is only called once, I think you are set as well: "ready" and "close" are only emitted once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't event handlers removed by the GC when the channel object is collected?
Perhaps/I hope so, I'm just never sure when exactly that happens. It's not synchronous like in C, Vala or Rust, AFAIK there's an actual GC running (similar to Python). This doesn't matter for this case, but it seems easy to have event handlers like systemd signals or D-Bus "PropertiesChanged" which wreak havoc when they happen multiple times and call setState().
Add the spawned PID to the channel's "ready" message. This will be useful for cockpit-project#21021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Add the spawned PID to the channel's "ready" message. This will be
useful for #21021
The first commit is unrelated for this PR, but you'll need it for the demo below. But having "once" event handlers is generally useful, so let's already stop typescript to yell at us about that.@ashley-cui you can test it with this demo, that'll show the pid when opening the terminal, and it goes away when you control-d it.