Skip to content

Commit

Permalink
Avoid I/O deadlock with conda
Browse files Browse the repository at this point in the history
`conda lock` may deadlock with a child `conda` process, it may be trying
to read from `stdout` while `conda` is trying to write to `stderr`
(blocked)

Reading from `stdout` in a separate thread allows `stderr` to be
consumed in the main thread.
  • Loading branch information
tadeu committed Jan 15, 2024
1 parent aa49149 commit 4a985bf
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions conda_lock/invoke_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil
import subprocess
import tempfile
import threading

from distutils.version import LooseVersion
from typing import IO, Dict, Iterator, List, Optional, Sequence, Union
Expand Down Expand Up @@ -116,14 +117,21 @@ def _invoke_conda(
) as p:
stdout = []
if p.stdout:
for line in _process_stdout(p.stdout):
logging.info(line)
stdout.append(line)

def read_stdout() -> None:
assert p.stdout is not None
for line in _process_stdout(p.stdout):
logging.info(line)
stdout.append(line)

stdout_thread = threading.Thread(target=read_stdout)
stdout_thread.start()
stderr = []
if p.stderr:
for line in p.stderr:
stderr.append(line)
logging.error(line.rstrip())
stdout_thread.join()

if check_call and p.returncode != 0:
raise subprocess.CalledProcessError(
Expand Down

0 comments on commit 4a985bf

Please sign in to comment.