Skip to content

Commit

Permalink
Merge pull request #1687 from rstudio/bugfix/positron-io-reset
Browse files Browse the repository at this point in the history
Fix Positron stdout redirection after Ipykernel closes
  • Loading branch information
t-kalinowski authored Oct 23, 2024
2 parents 19276b7 + 46a646f commit 5834f6a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions R/thread.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ py_run_file_on_thread <- function(file, ..., args = NULL) {
if (launching_lsp) {
main_dict <- py_eval("__import__('__main__').__dict__.copy()", FALSE)
py_get_attr(main_dict, "pop")("__annotations__")
# IPykernel will create a thread that redirects all output from fileno of
# the current sys.stdout and sys.stderr to its IO channels.
# This is not correctly cleaned up when IPykernel closes.
# To fix that, we set the IO streams to /dev/null before launching the kernel.
import("rpytools.run")$set_blank_io_streams()
}

import("rpytools.run")$run_file_on_thread(file, args, ...)
Expand Down
7 changes: 7 additions & 0 deletions inst/python/rpytools/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def _launch_lsp_server_on_thread(path, args):
return run_file_on_thread(path, args)


def set_blank_io_streams():
import sys
import os
sys.stdout = open(os.devnull, "w")
sys.stderr = open(os.devnull, "w")


def run_file_on_thread(path, argv=None, init_globals=None, run_name="__main__"):
# for now, leave sys.argv and sys.path permanently modified.
# Later, revisit if it's desirable/safe to restore after the initial
Expand Down

0 comments on commit 5834f6a

Please sign in to comment.