Skip to content

Commit

Permalink
Restore CTRL-C handling by setting new flag in libmamba (#340)
Browse files Browse the repository at this point in the history
Co-authored-by: jaimergp <jaimergp@users.noreply.github.com>
Co-authored-by: Jannis Leidel <jannis@leidel.info>
  • Loading branch information
3 people authored Nov 2, 2023
1 parent 5655bda commit 33e33e6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
2 changes: 2 additions & 0 deletions conda_libmamba_solver/mamba_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def set_channel_priorities(index: Dict[str, "_ChannelRepoInfo"], has_priority: b


def init_api_context() -> api.Context:
# This function has to be called BEFORE 1st initialization of the context
api.Context.use_default_signal_handler(False)
api_ctx = api.Context()

# Output params
Expand Down
19 changes: 19 additions & 0 deletions news/339-pthread-sigmask
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Do not use `libmamba`'s default signal handler so users can `Ctrl-C` from `conda`. (#337 via #340)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ addopts = [
"--strict-markers",
"--xdoctest-modules",
"--xdoctest-style=google",
"--reruns=3",
]

markers = [
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
conda-build
conda-forge::pytest-xprocess
conda-index
conda-build
conda-lock
# needed for many conda tests
flask
61 changes: 57 additions & 4 deletions tests/test_workarounds.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Copyright (C) 2022 Anaconda, Inc
# Copyright (C) 2023 conda
# SPDX-License-Identifier: BSD-3-Clause
import ctypes
import json
import signal
import subprocess as sp
import sys
from subprocess import PIPE, check_call, run
import time

import pytest
from conda.common.compat import on_win


def test_matchspec_star_version():
Expand All @@ -12,7 +18,7 @@ def test_matchspec_star_version():
We work around that with `.utils.safe_conda_build_form()`.
Reported in https://github.com/conda/conda/issues/11347
"""
check_call(
sp.check_call(
[
sys.executable,
"-m",
Expand All @@ -31,7 +37,7 @@ def test_matchspec_star_version():


def test_build_string_filters():
process = run(
process = sp.run(
[
sys.executable,
"-m",
Expand All @@ -44,7 +50,7 @@ def test_build_string_filters():
"numpy=*=*py38*",
"--json",
],
stdout=PIPE,
stdout=sp.PIPE,
text=True,
)
print(process.stdout)
Expand All @@ -56,3 +62,50 @@ def test_build_string_filters():
assert pkg["version"].startswith("3.8")
if pkg["name"] == "numpy":
assert "py38" in pkg["build_string"]


@pytest.mark.parametrize("stage", ["Collecting package metadata", "Solving environment"])
def test_ctrl_c(stage):
p = sp.Popen(
[
sys.executable,
"-m",
"conda",
"create",
"-p",
"UNUSED",
"--dry-run",
"--solver=libmamba",
"--override-channels",
"--channel=conda-forge",
"vaex",
],
text=True,
stdout=sp.PIPE,
stderr=sp.PIPE,
)
t0 = time.time()
while stage not in p.stdout.readline():
time.sleep(0.1)
if time.time() - t0 > 30:
raise RuntimeError("Timeout")

# works around Windows' awkward CTRL-C signal handling
# https://stackoverflow.com/a/64357453
if on_win:
try:
kernel = ctypes.windll.kernel32
kernel.FreeConsole()
kernel.AttachConsole(p.pid)
kernel.SetConsoleCtrlHandler(None, 1)
kernel.GenerateConsoleCtrlEvent(0, 0)
p.wait(timeout=30)
assert p.returncode != 0
assert "KeyboardInterrupt" in p.stdout.read() + p.stderr.read()
finally:
kernel.SetConsoleCtrlHandler(None, 0)
else:
p.send_signal(signal.SIGINT)
p.wait(timeout=30)
assert p.returncode != 0
assert "KeyboardInterrupt" in p.stdout.read() + p.stderr.read()

0 comments on commit 33e33e6

Please sign in to comment.