Log messages written from a helper thread appear in the wrong notebook cell (regression)
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 58/100
Research direction
Run the minimal three-cell example with ipykernel 6.28.0 and 6.29.0, focusing on Logger.write_from_queue, Logger.log, and the sys.stderr sink. Trace how output from the helper thread is associated with notebook cells. Done means messages from cell 3 and later cells appear in their responsible cell rather than the logger-initialization cell.
Written by the indexing model from the issue text.
Description
I recently found that, with some configurations, log messages emitted by Loguru would appear in the wrong Jupyter notebook cell: namely, the cell that initialized the logger, rather than the cell that was responsible for the log message. I opened Delgan/loguru#1427 to address this, but have since closed it, since the issue appears to be a regression in ipykernel, not in Loguru itself.
The problem appears for all versions of Loguru, but only for ipykernel >= 6.29. It is confined to the case where the logger is configured to write messages using a separate helper thread, which is useful when logging from multiple threads simultaneously. A minimal working example that illustrates the issue without the use of Loguru is the following:
(cell 1)
import sys
import multiprocessing
import threading
class Logger:
def __init__(self, sink):
self.sink = sink
self.queue = multiprocessing.SimpleQueue()
self.lock = threading.Lock()
self.thread = threading.Thread(target=self.write_from_queue, daemon=True)
self.thread.start()
def write_from_queue(self):
while True:
message = self.queue.get()
with self.lock:
self.sink.write(message)
def log(self, message):
self.queue.put(message)
(cell 2)
logger = Logger(sink=sys.stderr)
(cell 3)
logger.log("a message")
With ipykernel 6.28.0, the log message appears under cell 3, as intended, but with 6.29.0, it appears under cell 2 instead, and subsequent messages logged using the same logger will always appear under cell 2.
(Note: I tested with Jupyter Notebook 7.5.0 and Python 3.12.11 on Ubuntu 24.04.)
- Dominant language
- Python
- Stars
- 734
- Forks
- 411
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 9
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ipython/ipykernel
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
Difficulty 4/5 3-5 days Newbie friendliness 68/100
-
ipython/ipykernel#1550 · 1 comment · 1 reaction · 1 assignee ·
-
Difficulty 3/5 1-2 days Newbie friendliness 66/100
All issues in ipython/ipykernel
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·