Skip to content

console language projection should track its switch until it lands in the input buffer #19258

Open

Description

@kevinushey

Summary

module_context::adaptToLanguage() (SessionModuleContext.cpp) switches the console between R and Python by enqueueing reticulate::repl_python() or quit through the console input service. The request lands in the console input buffer a few milliseconds later. Until then neither the buffer nor the console's real state shows the switch, so the function keeps a projected language (s_projectedLanguage) and the module prompt hook clears it: the next prompt means the console is idle and its real state is current again.

The projection has no notion of whether its switch is still in transit, already buffered, consumed, or discarded. Two failure modes follow:

  1. Stale projection after a discard. If the switch has landed in the buffer and the buffer is then cleared (an interrupt, or an uncaught error with discard_pending_console_input_on_error on) without a prompt following, the projection says "Python" while the console is really in R. The next Python submission skips repl_python(), and Python that is valid R (x = 42) runs silently in R. Since keep serving requests while r runs code from the idle event loop #19237 input can queue behind a callback running from the idle event loop, and the prompt reissued when the callback returns runs no module hook, this is reachable: interrupt a long callback, then run a Python chunk. keep serving requests while r runs code from the idle event loop #19237 mitigates it by dropping the projection when input has been discarded since it was made.

  2. Forgotten switch in transit. The mitigation (and any clear-on-discard) also drops a projection whose switch has not landed yet. If adaptToLanguage("R") runs in those few milliseconds, it sees the console's real state (R) and sends no quit; the late switch then puts the console in Python and R code runs there.

Proposed fix

Track the switch itself rather than a projected language: count switches enqueued, and have the console input layer report when one lands in the buffer (the RConsoleInput built by enqueueConsoleInput can carry a marker in its console field, or the input service can report arrival). A clear of the buffer then invalidates only switches that were actually in it; switches still in transit keep the projection. With that, both failure modes close, and the mitigation in #19237 can go.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions