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

Run cleanup handers first, then post processing #692

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

afresh1
Copy link
Contributor

@afresh1 afresh1 commented Feb 9, 2023

The FCGI::ProcManager pm_post_dispatch method may exit if it got a signal. Currently that will exit before handling the cleanup handlers. This is similar to harakiri, and the PSGI::Extensions documentation suggests that "it SHOULD [be implemented] in a way that cleanup handlers run before harakiri". Moving the cleanup handlers before the call to pm_post_dispatch should behave more correctly.

Currently some post processing hooks are checks like memory check, if a
worker hits a memory limit, it will try to recycle the worker. In that
case, any post processing cleanup handling code wouldn't have a chance
to run. This is to fix the execution order.
@miyagawa
Copy link
Member

miyagawa commented Feb 9, 2023

Hm, the comment suggests that when the proc manager exists that sends the TERM signal to workers and that is being handled in the cleanup handler. Isn't that the case you're talking about?

@afresh1
Copy link
Contributor Author

afresh1 commented Feb 9, 2023

Yes, when the manager exits, it sends its workers a TERM signal. That gets handled in the worker by the pm_post_dispatch method, which unfortunately currently happens before we process the cleanup handlers. The patch handles that signal after the cleanup handlers.

The cleanup handlers themselves have extra protection from a TERM signal as it's possible to either not have a manager at all. or to disable the signal handling in the manager. In either of those cases, the cleanup watches for that TERM signal and enables harakiri, so that it will exit whether there is a manager or not.

@miyagawa
Copy link
Member

miyagawa commented Feb 9, 2023

just curious, is there a reason not to move this after harakiri handling code either?

@afresh1
Copy link
Contributor Author

afresh1 commented Feb 9, 2023

At work we have some legacy hooks that happens in a ProcManager subclass' pm_post_dispatch before we call the parent method that exits. Moving this after the harakiri might cause that to get skipped and we'd have to refactor some things that aren't a priority at the moment.

@miyagawa
Copy link
Member

miyagawa commented Feb 9, 2023

Can you supply a unit test that shows the original problem?

@afresh1
Copy link
Contributor Author

afresh1 commented Feb 9, 2023

I expect we can figure that out.

@miyagawa
Copy link
Member

miyagawa commented Feb 9, 2023

Putting the discussion aside, you seem to be relying on some undocumented behaviors and ProcManager internals a lot. I'd fork the entire code into your own module and use it instead of using this, at least for now. I understand your desire to upstream the change and avoid a potential divergence, but this code has not been updated for years, so...

@afresh1
Copy link
Contributor Author

afresh1 commented Feb 9, 2023

We currently did fork and have an override with this change, but yes, would be much nicer to get it upstream and remove that.

Hu Yin added 3 commits February 14, 2023 15:21
Manually set n_process=0 and call hanlding_init to avoid manager forking
…al handling

Merge in GITHUB/plack from test-post-processing-order-fix to fix_post_processing_exe_order

* commit '0b4648d13da7a2a1c90dce1dff0dba03e7ac293e':
  Update POD about cleanup hook
  Test cleanup handler and Proc Manager Term signal handling
@afresh1
Copy link
Contributor Author

afresh1 commented Feb 24, 2023

You probably got a notification, but we pushed up a test for this.

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.

2 participants