Skip to content

forcibly close DB and cache socket connection post-fork - #8580

Merged
softwarefactory-delete-me[bot] merged 1 commit into
ansible:develfrom
ryanpetrello:fork-race
Jan 15, 2021
Merged

softwarefactory-delete-me[bot] merged 1 commit into
ansible:develfrom
ryanpetrello:fork-race

Conversation

@ryanpetrello

@ryanpetrello ryanpetrello commented Nov 12, 2020 •

Copy link
Copy Markdown
Contributor

we've seen evidence of a race condition on fork for awx.conf.model.Setting
access; in the past, we've attempted to solve this by explicitly closing
connections pre-fork, but we've seen evidence that this isn't always
good enough

this patch is an attempt to close connections post-fork so that sockets
aren't inherited post fork, leading to bizarre race conditions in
setting access

Comment thread awx/conf/settings.py
def __clean_on_fork__(self):
pid = os.getpid()
if pid not in self.__dict__['__forks__']:
self.__dict__['__forks__'][pid] = True

@ryanpetrello ryanpetrello Nov 12, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We have to access this via __dict__ instead of getattr to avoid a recursive loop (because __getattr__ calls this function via _get_local).

@ryanpetrello ryanpetrello Nov 12, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also tested this out by printing this dict in a tight loop of awx-python forks, and the data structure for each process never grows beyond two keys (the parent pid, and the current process pid).

So in effect, this branch is entered one time when a child process does getattr(settings, ...), and then after that, it's a no-op.

@ryanpetrello
ryanpetrello requested a review from matburt November 12, 2020 18:40
@softwarefactory-delete-me

Copy link
Copy Markdown
Contributor

Build failed.

@softwarefactory-delete-me

Copy link
Copy Markdown
Contributor

Build succeeded.

we've seen evidence of a race condition on fork for awx.conf.Setting
access; in the past, we've attempted to solve this by explicitly closing
connections pre-fork, but we've seen evidence that this isn't always
good enough

this patch is an attempt to close connections post-fork so that sockets
aren't inherited post fork, leading to bizarre race conditions in
setting access
@softwarefactory-delete-me

Copy link
Copy Markdown
Contributor

Build succeeded.

@matburt matburt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This harkens back to when Ansible would change pids occasionally on us during event processing years ago.

@softwarefactory-delete-me

Copy link
Copy Markdown
Contributor

Build succeeded (gate pipeline).

@softwarefactory-delete-me
softwarefactory-delete-me Bot merged commit 684998c into ansible:devel Jan 15, 2021
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.

4 participants