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

Fix sigint handling when running in helper mode #869

Merged
merged 7 commits into from
Jun 20, 2023
Merged

Conversation

olsen232
Copy link
Collaborator

Description

If kart.c receives a SIGINT while its waiting for python kart_cli, we need to SIGINT python kart_cli too.

@olsen232 olsen232 requested review from craigds and rcoup June 14, 2023 05:04
cli_helper/kart.c Outdated Show resolved Hide resolved
kart/__init__.py Show resolved Hide resolved
Copy link
Member

@rcoup rcoup left a comment

Choose a reason for hiding this comment

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

If the only solution is lsof, we need to add it as a dependency of our packages. Would prefer a more standalone solution if there is a practical/sane one.

cli_helper/kart.c Outdated Show resolved Hide resolved
kart/__init__.py Show resolved Hide resolved
cli_helper/kart.c Outdated Show resolved Hide resolved
@olsen232 olsen232 mentioned this pull request Jun 14, 2023
8 tasks
Copy link
Member

@craigds craigds left a comment

Choose a reason for hiding this comment

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

looks much better now 👍

kart/helper.py Outdated
# Join the process group of the calling process - so that if they get killed, we get killed to.
os.setpgid(0, calling_environment["pid"])
os.environ["_KART_PGID_SET"] = "1"
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

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

I suspect this could catch a more specific exception class (OSError? or one of the subclasses: https://docs.python.org/3/library/exceptions.html#os-exceptions)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the possible error codes from setpgid() are all mapped to subclasses of OSError.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 285 to 286
char *socket_filename = malloc(strlen(getenv("HOME")) + strlen(".kart.1234567890.socket") + 2);
sprintf(socket_filename, "%s/.kart.%d.socket", getenv("HOME"), getsid(0));
Copy link
Member

Choose a reason for hiding this comment

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

sprintf is evil 👿

Suggested change
char *socket_filename = malloc(strlen(getenv("HOME")) + strlen(".kart.1234567890.socket") + 2);
sprintf(socket_filename, "%s/.kart.%d.socket", getenv("HOME"), getsid(0));
size_t socket_filename_sz = strlen(getenv("HOME")) + strlen("/.kart..socket") + sizeof(pid_t) * 3 + 1;
char *socket_filename = malloc(socket_filename_sz);
int r = snprintf(socket_filename, socket_filename_sz, "%s/.kart.%d.socket", getenv("HOME"), getsid(0));
if (r < 0 || (size_t) r >= socket_filename_sz)
{
fprintf(stderr, "Error allocating socket filename\n");
exit(1);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@rcoup rcoup left a comment

Choose a reason for hiding this comment

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

LGTM after tidyups

Comment on lines 117 to 120
@pytest.mark.parametrize("use_helper", [False, True])
def test_sigint_handling_unix(use_helper, tmp_path):
if is_windows:
return
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@pytest.mark.parametrize("use_helper", [False, True])
def test_sigint_handling_unix(use_helper, tmp_path):
if is_windows:
return
@pytest.mark.skipif(os.name == "nt")
@pytest.mark.parametrize("use_helper", [False, True])
def test_sigint_handling_unix(use_helper, tmp_path):

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

kart/helper.py Outdated
# Join the process group of the calling process - so that if they get killed, we get killed to.
os.setpgid(0, calling_environment["pid"])
os.environ["_KART_PGID_SET"] = "1"
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
except Exception as e:
except OSError as e:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@olsen232 olsen232 merged commit b07ec84 into master Jun 20, 2023
32 checks passed
@olsen232 olsen232 deleted the fix-sigint branch June 20, 2023 09:09
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

3 participants