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

Abort proxy when stdin gets closed #3638

Merged
merged 3 commits into from
Jun 28, 2024

Conversation

jonatanklosko
Copy link
Contributor

There is no guarantee that a OS process spawning fly proxy ... will terminate it, however the stdin is always closed when the parent terminates. In order to avoid zombie processes, we can watch stdin and when it gets closed we terminate the program. The original motivation is spawning fly proxy from Erlang VM.

For more context see a similar PR on esbuild: evanw/esbuild#1449.

// Note that we don't do this when stdin is TTY, because that prevents
// the process from being moved to a background job on Unix.
// See https://github.com/brunch/brunch/issues/998.
func watchStdinAndAbortOnClose(ctx context.Context) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed only the fly proxy specifically, but there are a few that could probably use the same logic, like fly machine api-proxy and fly redis proxy. I can enable the watching there too, just let me know what is the best place to put this function to share across commands.

Copy link
Member

Choose a reason for hiding this comment

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

hi @jonatanklosko, sorry I haven't replied, I saw it days ago and took a step back because my first reaction was "what a gross hack" and preferred to give it some time to settle in my mind 😄

I mean, it is kind of clever thing to do but also could be a source of gotcha moment for someone that doesn't expect the proxy to terminate out of nowhere. Are you aware of any tool that does this trick?

I think we can work towards merging it anyways but:

  • Instead of making it the default, can this terminate-on-stdin-close mode be behind a command line flag?
  • Instead of calling os.Exit(), can it cancel the context used by the proxy to gracefully stop it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dangra no worries, it seems like a hack, but many command line tools actually do that, like cat, fsevents, esbuild, webpack, postcss.

A flag sounds good to me, that's what webpack does.

Should I share and apply this to other proxies, or do you prefer to keep it for this one specifically?

can it cancel the context used by the proxy to gracefully stop it?

How would the cancel look like? :)

Copy link
Member

Choose a reason for hiding this comment

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

A flag sounds good to me, that's what webpack does.

sgtm!

Should I share and apply this to other proxies, or do you prefer to keep it for this one specifically?

what other proxies do you refer to?

How would the cancel look like? :)

I realize this a nitpick and won't make much difference for this case, but I'd derive the context passed to proxy.Connect() using context.WithCancelCause(); and then call the cancel function instead of os.Exit().

That should allow the proxy to gracefully shutdown instead of abruptly exit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what other proxies do you refer to?

This is fly proxy, I noticed the proxy is also used in fly machine api-proxy and fly redis proxy, so I wonder if all of these should have the flag for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW I only need this for fly proxy, so I'm happy having the flag just here.

Copy link
Member

Choose a reason for hiding this comment

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

only this one is fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok! I added the flag and context cancel, let me know if there's anything else :)

@jonatanklosko
Copy link
Contributor Author

Kindly ping :)

@dangra dangra merged commit 3501aeb into superfly:master Jun 28, 2024
1 check passed
@dangra
Copy link
Member

dangra commented Jun 28, 2024

Good stuff @jonatanklosko . thanks :)

@jonatanklosko jonatanklosko deleted the jk-watch-stdin branch June 29, 2024 07:16
@jonatanklosko
Copy link
Contributor Author

Fantastic, thanks for the help!

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.

None yet

2 participants