Skip to content

Comments

Replace daemon Thread in dev-mode of breeze start-airflow with forking#31403

Merged
potiuk merged 2 commits intoapache:mainfrom
potiuk:fix-stopping-webpack-in-dev-mode
May 19, 2023
Merged

Replace daemon Thread in dev-mode of breeze start-airflow with forking#31403
potiuk merged 2 commits intoapache:mainfrom
potiuk:fix-stopping-webpack-in-dev-mode

Conversation

@potiuk
Copy link
Member

@potiuk potiuk commented May 18, 2023

When you run the dev mode for Breeze start-airlfow the webpack to compile the assets is run in the background and should be stopped as soon as the main process leaves. This has been done via daemon Thread, however it turns out that when daemon thread gets stopped at exit, it is stopped pretty abruptly. Quoting

https://docs.python.org/3/library/threading.html ::

Note Daemon threads are abruptly stopped at shutdown. Their resources
(such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an Event.

This also means that if the daemon threds starts a subprocess, the thread is killed but the subprocess is not - the subprocess gets orphaned and gets adopted by init process and continues running.

Tis PR replaces the mechanism with forking. Instead of running thread, it will fork a new process that will run the compilation (including starting the subprocess that runs it). It will also change group for the started forked process to be the same as the new pid, which creates a new process group and register an atexit method to send TERM to all the processes belonging to that grop, so even if the node process that runs compilation does not propagate the TERM signal (it does not) to the webpack processes, all the webpack processes share the same process group and get the SIGTERM signal.

Fixes: #31384


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

When you run the dev mode for Breeze start-airlfow the webpack to
compile the assets is run in the background and should be stopped
as soon as the main process leaves. This has been done via daemon
Thread, however it turns out that when daemon thread gets stopped
at exit, it is stopped pretty abruptly. Quoting

https://docs.python.org/3/library/threading.html ::

> Note Daemon threads are abruptly stopped at shutdown. Their resources
(such as open files, database transactions, etc.) may not be released
properly. If you want your threads to stop gracefully, make them
non-daemonic and use a suitable signalling mechanism such as an Event.

This also means that if the daemon threds starts a subprocess, the
thread is killed but the subprocess is not - the subprocess gets
orphaned and gets adopted by init process and continues running.

Tis PR replaces the mechanism with forking. Instead of running
thread, it will fork a new process that will run the compilation
(including starting the subprocess that runs it). It will also
change group for the started forked process to be the same as
the new pid, which creates a new process group and register
an atexit method to send TERM to all the processes belonging to
that grop, so even if the node process that runs compilation does
not propagate the TERM signal (it does not) to the webpack processes,
all the webpack processes share the same process group and get the
SIGTERM signal.

Fixes: apache#31384
@potiuk potiuk force-pushed the fix-stopping-webpack-in-dev-mode branch from 4cb45fd to 8c33708 Compare May 18, 2023 23:58
@potiuk potiuk requested a review from uranusjr May 18, 2023 23:58
Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

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

Thank you !

just a minor typo to fix, LGTM

Co-authored-by: Pierre Jeambrun <pierrejbrun@gmail.com>
@potiuk potiuk merged commit ac00547 into apache:main May 19, 2023
@potiuk potiuk deleted the fix-stopping-webpack-in-dev-mode branch May 19, 2023 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Breeze asset compilation causing OOM on dev-mode

3 participants