posit-dev / posit-dev/ark

`Console::get_mut()` should use `CONSOLE.try_with()`

Open
#1,320 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
337
Forks
32
Avg merge
8d 17h
Merged PRs (30d)
11

Description

I just ran into https://github.com/posit-dev/ark/pull/1222/changes/b6e980d5efb7f4ce865bfc8cd58792b6da6139bc with this panic message

cannot access a Thread Local Storage value during or after destruction: AccessError

What happened was:

  • CONSOLE was destructing as R shut down
  • CONSOLE.pending_futures was destructed via CONSOLE destructing
  • One of the pending_futures was created with capture support, i.e. with ConsoleOutputCapture enabled
  • ConsoleOutputCapture::drop was run as the future destructed. This tried to re-access CONSOLE via Console::get_mut(). But CONSOLE is destructing! So we get a panic.

We should analyze this and figure out the most robust solution.

For my use case, I was accidentally holding a permanently running RTask with capture support, so the easy solution for me was to just turn off capture support, avoiding ConsoleOutputCapture::drop from running.

But I think in theory that even short running futures with capture support that just so happen to be in pending_futures when R restarts and CONSOLE destructs can cause this to happen.

So maybe we need to be able to call something like Console::try_get_mut() from this location, which would use CONSOLE.try_with() instead of CONSOLE.with_borrowed_mut(). I think you can build a form of try_with_borrowed_mut() by looking at the definition of with_borrowed_mut() and replacing the .with() with .try_with().

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

Start with Console::get_mut(), with_borrowed_mut(), and ConsoleOutputCapture::drop, then trace how pending_futures are destroyed while CONSOLE is shutting down. Reproduce or test the capture-enabled future cleanup path and ensure destruction does not panic when CONSOLE is unavailable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.