Skip to content

Race condition between schedule() and stop() methods in IOPubThread #1612

Backlog

Description

@tovrstra

This issue surfaced when running several notebooks in parallel. I expect the race to be more common in computational workflows that execute notebooks (with parameters) in parallel, as in papermill or stepup-notebook. (I'm currently developing the latter.) Here is an example occurence in a GitHub workflow used for testing StepUp Notebook:

https://github.com/reproducible-reporting/stepup-notebook/actions/runs/46718657063/job/142897670684

It wasn't trivial to find the root cause, and the following line is only an indirect symptom:

Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)

With some AI help, I managed to create a minimal reproducer. It is essentially calling IOPubThread.schedule() a massive number of times, to increase the odds of executing IOPubThread.close() after if self.thread.is_alive() in schedule() but before the code in the if-block. The thread is then no longer alive while the code being executed assumes that it is.

#!/usr/bin/env python3
import subprocess
from concurrent.futures import ThreadPoolExecutor

from jupyter_client.manager import KernelManager

SPAMMER = """
import sys, threading
def spam():
    try:
        while True:
            sys.stdout.pub_thread.schedule(lambda: None)
    except Exception:
        pass
for _ in range(4):
    threading.Thread(target=spam, daemon=True).start()
"""

def trial(i):
    km = KernelManager(kernel_name="python3", transport="ipc", ip=f"/tmp/repro-{i}")
    # Show more output in the terminal.
    km.start_kernel(stderr=subprocess.STDOUT, extra_arguments=["--IPKernelApp.capture_fd_output=False"])
    # Get process while it still exists.
    proc = km.provisioner.process
    kc = km.client()
    kc.wait_for_ready()
    # The code in SPAMMER spams the schedule() method to trigger the race reliably.
    kc.execute_interactive(SPAMMER, timeout=60)
    km.shutdown_kernel()
    print(f"trial {i}: kernel exit code {proc.wait()}", flush=True)

# Some system load is needed to trigger the issue,
# which is similar to a busy GitHub workflow runner.
with ThreadPoolExecutor(16) as pool:
    list(pool.map(trial, range(64)))

Output with the latest main branch of ipykernel as this writing (7940a09) on Fedora Linux with Python 3.15:

Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 4: kernel exit code 0
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 2: kernel exit code 0
trial 10: kernel exit code 0
trial 6: kernel exit code 0
trial 7: kernel exit code 0
trial 11: kernel exit code 0
trial 12: kernel exit code 0
trial 1: kernel exit code 0
trial 15: kernel exit code 0
trial 14: kernel exit code -6
trial 3: kernel exit code -6
trial 8: kernel exit code -6
trial 13: kernel exit code -6
trial 9: kernel exit code -6
trial 5: kernel exit code -6
trial 0: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 21: kernel exit code 0
trial 19: kernel exit code 0
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 17: kernel exit code -6
trial 18: kernel exit code -6
trial 16: kernel exit code -6
trial 22: kernel exit code -6
trial 29: kernel exit code 0
trial 23: kernel exit code -6
trial 24: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 20: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 27: kernel exit code 0
trial 31: kernel exit code 0
trial 28: kernel exit code 0
trial 25: kernel exit code -6
trial 26: kernel exit code -6
trial 30: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 34: kernel exit code 0
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 39: kernel exit code -6
trial 40: kernel exit code 0
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 43: kernel exit code -6
trial 44: kernel exit code 0
trial 32: kernel exit code -6
trial 33: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 46: kernel exit code 0
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 36: kernel exit code -6
trial 35: kernel exit code -6
trial 38: kernel exit code -6
trial 42: kernel exit code -6
trial 41: kernel exit code -6
trial 37: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 45: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 47: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 52: kernel exit code 0
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 48: kernel exit code -6
trial 53: kernel exit code 0
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 49: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 51: kernel exit code -6
Assertion failed: pfd.revents & POLLIN (src/signaler.cpp:238)
trial 62: kernel exit code -6
trial 54: kernel exit code -6
trial 55: kernel exit code -6
trial 58: kernel exit code -6
trial 50: kernel exit code -15
trial 56: kernel exit code -6
trial 57: kernel exit code -6
trial 63: kernel exit code 0
trial 59: kernel exit code -6
trial 61: kernel exit code -6
trial 60: kernel exit code -6

I think the solution is to introduce a lock that prevents concurrent execution of some of the code in schedule() and stop(). I'll open a PR soon.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions