python-trio / python-trio/trio

run_process ignores thread context and spawns from threadpool

Open
#3,360 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

Hi,

run_process uses trio.to_thread.run_sync to spawn the subprocess; this means that the spawned process will inherit various context from a different thread. In my case this means it spawns in the wrong (network) namespace (see setns(2) - "reassociate thread with a namespace").

This might affect other things as well (pthreads(7) lists (among other things) capabilities and CPU affinity).

  1. the docs should mention this behavior
  2. it would be nice to have a flag to "spawn from current thread"

I'm also open to good suggestions how to work around this problem :)
(I might try prefixing the target command with ["/usr/bin/nsenter", "--target", threading.get_native_id(), "-n", "--"], or copy the trio run_process/open_process functions).

cheers,
Stefan

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the run_process and open_process entry points, including their use of trio.to_thread.run_sync, and review the linked namespace and pthread documentation. Confirm which thread context is inherited, then define the documented behavior and the scope of a current-thread spawning option or accepted workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.