Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_print_to_correct_cell_from_child_thread #1312

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

davidbrochart
Copy link
Collaborator

The parent thread has to live for the duration of the child thread, otherwise the parent thread exits before the child thread and disappears from the active threads, causing it to be removed from thread_to_parent and eventually leading to messages not being routed to the right cell:

thread_to_parent_header = stream._thread_to_parent_header
for identity in list(thread_to_parent_header.keys()):
if identity not in active_threads:
try:
del thread_to_parent_header[identity]
except KeyError:
pass
thread_to_parent = stream._thread_to_parent
for identity in list(thread_to_parent.keys()):
if identity not in active_threads:
try:
del thread_to_parent[identity]
except KeyError:
pass

@krassowski Could you confirm? I saw that while working on #1291.

Thread(target=child_target).start()
sleep({interval * iterations})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it also work if in addition to this sleep you keep the sleep({interval}) above? I wonder if that was here for a reason. In other words, I think that adding the sleep at the end is fine, I am less confident about the function of the sleep that was removed (as this can change some edge case related to execution order).

Further, should we save a reference to the thread in a local variable before calling sleep()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 41b31c5 I added back the first sleep, and replaced the other sleep with a thread.join(), since it's essentially the same.

@minrk
Copy link
Member

minrk commented Dec 20, 2024

I think also related to this is #1289 . Most of the time, in my experience, routing output to the cell that happened to spawn the thread not the right thing to do, and results in quite a bit of mysteriously lost and out-of-order output.

@krassowski
Copy link
Member

krassowski commented Dec 20, 2024

Thanks for linking #1289, I missed it when it was opened (despite the mention!). I see your point. I think that usually routing to the spawning cell is the right thing to do, but will happily agree that there should be a way to opt out for the scenarios that you present.

@davidbrochart
Copy link
Collaborator Author

Failure is unrelated:

FAILED tests/test_subshells.py::test_run_concurrently_sequence[True-are_subshells0] - AssertionError: assert 'error' == 'ok'

Copy link
Member

@krassowski krassowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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

Successfully merging this pull request may close these issues.

3 participants