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

REPL: spawn prompt! to avoid blocking typing, fix pkg repl transition sequencing #54785

Merged
merged 2 commits into from
Jun 18, 2024

Conversation

IanButterworth
Copy link
Member

@IanButterworth IanButterworth commented Jun 13, 2024

Based on #54760 (comment) by @Keno

This changes the prompt! design to not consume on the two tasks until inside the lock.

However, with this design I find that there's a 50% chance that interactive startup with -t2 will block typing while Pkg loads.

As an attempt to avoid typing blocking in this design, suggested by @vchuravy I've made repl_main run on a non-sticky task and changing any repl @async calls to @spawn to avoid thread-pinning the repl task.

But the 50% chance of blocking typing while Pkg loads remains.

It seems the repl task is still not freely switching threads.

Spawning prompt! specifically avoids typing blocking, because the general repl task is made sticky by use of _wait2 as well as the @asyncs.

@IanButterworth IanButterworth added the REPL Julia's REPL (Read Eval Print Loop) label Jun 13, 2024
t = Threads.@spawn while true
wait(s.async_channel)
status = @lock l begin
fcn = take!(s.async_channel)
Copy link
Member

Choose a reason for hiding this comment

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

If the actual take!ing isn't happening on the main task, then the main task needs to terminate this task while holding the lock before returning.

Copy link
Member Author

@IanButterworth IanButterworth Jun 13, 2024

Choose a reason for hiding this comment

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

You mean something like close the channel on the main task and check after wait?

        t = Threads.@spawn while true
            try
                wait(s.async_channel)
            catch
                isopen(s.async_channel) || break
                rethrow()
            end
            status = @lock l begin
                fcn = take!(s.async_channel)

Copy link
Member Author

Choose a reason for hiding this comment

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

But then again, if every call to prompt! is creating this task, then we should tidy up before returning from prompt!, so it should close and reopen s.async_channel but that seems racy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Pushed a fix

@IanButterworth IanButterworth force-pushed the ib/repl_prompt_tweak branch 2 times, most recently from c99aaf4 to 0a7c5a9 Compare June 13, 2024 19:07
@IanButterworth IanButterworth changed the title REPL: make the repl task non-sticky, fix pkg repl transition sequencing REPL: spawn prompt! to avoid blocking typing, fix pkg repl transition sequencing Jun 13, 2024
@IanButterworth IanButterworth added the backport 1.11 Change should be backported to release-1.11 label Jun 13, 2024
@IanButterworth
Copy link
Member Author

This should be ready for review. @Keno & @vtjnash it'd be good to make sure that you're both happy with this, and that it's not likely to resurface #54754

@Keno
Copy link
Member

Keno commented Jun 14, 2024

I think this is probably ok. I don't fundamentally agree that the two-mutators design is any simpler than waiting for event readiness, but at this point, I'm not seeing any obvious issues.

@IanButterworth
Copy link
Member Author

@vtjnash bump for review. It'd be great to get this settled and in 1.11.0-rc1 along with the rest of this work

@IanButterworth
Copy link
Member Author

I'm going to merge this as I think we can agree that it makes more sense than what's on master, and it should go in alongside the other work for the 1.11-rc

@IanButterworth IanButterworth merged commit 6f39acb into JuliaLang:master Jun 18, 2024
7 checks passed
@IanButterworth IanButterworth deleted the ib/repl_prompt_tweak branch June 18, 2024 19:22
IanButterworth added a commit that referenced this pull request Jun 18, 2024
…ing (#54785)

Based on #54760 (comment)

This changes the `prompt!` design to not consume on the two tasks until
inside the lock and spawns the two tasks to avoid blocking typing
@IanButterworth IanButterworth mentioned this pull request Jun 18, 2024
60 tasks
KristofferC pushed a commit that referenced this pull request Jun 19, 2024
…ing (#54785)

Based on #54760 (comment)

This changes the `prompt!` design to not consume on the two tasks until
inside the lock and spawns the two tasks to avoid blocking typing
@Keno
Copy link
Member

Keno commented Jun 20, 2024

Started seeing this after this PR:

Stacktrace:
  [1] try_yieldto(undo::typeof(Base.ensure_rescheduled))
    @ Base ./task.jl:1114
  [2] wait()
    @ Base ./task.jl:1178
  [3] wait(c::Base.GenericCondition{ReentrantLock}; first::Bool)
    @ Base ./condition.jl:139
  [4] wait
    @ ./condition.jl:134 [inlined]
  [5] take_buffered(c::Channel{Any})
    @ Base ./channels.jl:493
  [6] take!
    @ ./channels.jl:487 [inlined]
  [7] repl_backend_loop(backend::REPL.REPLBackend, get_module::Function)
    @ REPL ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/REPL.jl:355
  [8] start_repl_backend(backend::REPL.REPLBackend, consumer::Any; get_module::Function)
    @ REPL ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/REPL.jl:345
  [9] run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool, backend::Any)
    @ REPL ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/REPL.jl:501
 [10] run_repl(repl::REPL.AbstractREPL, consumer::Any)
    @ REPL ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/REPL.jl:487
 [11] (::Base.var"#1157#1159"{Bool, Symbol, Bool})(REPL::Module)
    @ Base ./client.jl:448
 [12] #invokelatest#2
    @ ./essentials.jl:1045 [inlined]
 [13] invokelatest
    @ ./essentials.jl:1042 [inlined]
 [14] run_main_repl(interactive::Bool, quiet::Bool, banner::Symbol, history_file::Bool)
    @ Base ./client.jl:432
 [15] repl_main
    @ ./client.jl:568 [inlined]
 [16] _start()
    @ Base ./client.jl:543

    nested task error: TaskFailedException
    Stacktrace:
     [1] wait(t::Task; throw::Bool)
       @ Base ./task.jl:380
     [2] wait
       @ ./task.jl:376 [inlined]
     [3] fetch
       @ ./task.jl:542 [inlined]
     [4] prompt!(term::REPL.Terminals.TextTerminal, prompt::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
       @ REPL.LineEdit ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/LineEdit.jl:2877
     [5] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
       @ REPL.LineEdit ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/LineEdit.jl:2743
     [6] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
       @ REPL ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/REPL.jl:1493
     [7] (::REPL.var"#75#81"{REPL.LineEditREPL, REPL.REPLBackendRef})()
       @ REPL ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/REPL.jl:498

        nested task error: ArgumentError: IOBuffer not marked
        Stacktrace:
         [1] reset
           @ ./io.jl:1429 [inlined]
         [2] reset
           @ ./stream.jl:1489 [inlined]
         [3] peek(s::Base.TTY, ::Type{Char})
           @ Base ./stream.jl:1496
         [4] peek(t::REPL.Terminals.TTYTerminal, ::Type{Char})
           @ REPL.Terminals ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/Terminals.jl:161
         [5] (::REPL.LineEdit.var"#282#284"{REPL.Terminals.TTYTerminal, REPL.LineEdit.ModalInterface, REPL.LineEdit.MIState, ReentrantLock, REPL.LineEdit.Prompt})()
           @ REPL.LineEdit ~/julia/usr/share/julia/stdlib/v1.12/REPL/src/LineEdit.jl:2843      

@nhz2
Copy link
Contributor

nhz2 commented Jun 20, 2024

This seems similar to the error I was seeing here: JuliaIO/TranscodingStreams.jl#224

@IanButterworth
Copy link
Member Author

Proposing reverting this #54862

@nhz2
Copy link
Contributor

nhz2 commented Jun 20, 2024

I think the underlying issue is that the default peek:

julia/base/io.jl

Lines 341 to 347 in 0a491e0

function peek(s::IO, ::Type{T}) where T
mark(s)
try read(s, T)::T
finally
reset(s)
end
end

removes the mark on the stream, by calling reset.

So if a function calls peek after marking the stream, that mark is unexpectedly lost.

read(io, ::Char) internally uses peek(io, ::UInt8) if reading non ascii:

peek(io) & 0xc0 == 0x80 || break

so depending on the IO type and data, it may or may not remove the mark from the stream.

@IanButterworth
Copy link
Member Author

Ah. I can now reproduce this on master if I type an emoji into the repl.

@IanButterworth
Copy link
Member Author

@nhz2 that does seem incorrect, but I've not been able to reduce it to a simple example. If you have, I think we should open an issue.

KristofferC added a commit that referenced this pull request Jun 25, 2024
Backported PRs:
- [x] #54361 <!-- [LBT] Upgrade to v5.9.0 -->
- [x] #54474 <!-- Unalias source from dest in copytrito -->
- [x] #54548 <!-- Fixes for bitcast bugs with LLVM 17 / opaque pointers
-->
- [x] #54191 <!-- make `AbstractPipe` public -->
- [x] #53402 <!-- Add `jl_getaffinity` and `jl_setaffinity` -->
- [x] #53356 <!-- Rename at-scriptdir project argument to at-script and
search upwards for Project.toml -->
- [x] #54545 <!-- typeintersect: fix incorrect innervar handling under
circular env -->
- [x] #54586 <!-- Set storage class of julia globals to dllimport on
windows to avoid auto-import weirdness. Forward port of #54572 -->
- [x] #54587 <!-- Accomodate for rectangular matrices in `copytrito!`
-->
- [x] #54617 <!-- CLI: Use `GetModuleHandleExW` to locate libjulia.dll
-->
- [x] #54605 <!-- Allow libquadmath to also fail as it is not available
on all systems -->
- [x] #54634 <!-- Fix trampoline assembly for build on clang 18 on apple
silicon -->
- [x] #54635 <!-- Aggressive constprop in trevc! to stabilize triangular
eigvec -->
- [x] #54645 <!-- ensure we set the right value to gc_first_tid -->
- [x] #54554 <!-- make elsize public -->
- [x] #54648 <!-- Construct LazyString in error paths for tridiag -->
- [x] #54658 <!-- fix missing uuid check on extension when finding the
location of an extension -->
- [x] #54594 <!-- Switch to Pkg mode prompt immediately and load Pkg in
the background -->
- [x] #54669 <!-- Improve error message in inplace transpose -->
- [x] #54671 <!-- Add boundscheck in bindingkey_eq to avoid OOB access
due to data race -->
- [x] #54672 <!-- make: Fix `sed` command for LLVM libraries with no
symbol versioning -->
- [x] #54624 <!-- more precise aliasing checks for SubArray -->
- [x] #54679 <!-- 🤖 [master] Bump the Distributed stdlib from 6a07d98 to
6c7cdb5 -->
- [x] #54604 <!-- Fix tbaa annotation on union selector bytes inside of
structs -->
- [x] #54690 <!-- Fix assertion/crash when optimizing function with dead
basic block -->
- [x] #54704 <!-- LazyString in reinterpretarray error messages -->
- [x] #54718 <!-- fix prepend StackOverflow issue -->
- [x] #54674 <!-- Reimplement dummy pkg prompt as standard prompt -->
- [x] #54737 <!-- LazyString in interpolated error messages involving
types -->
- [x] #54642 <!-- Document GenericMemory and AtomicMemory -->
- [x] #54713 <!-- make: use `readelf` for LLVM symbol version detection
-->
- [x] #54760 <!-- REPL: improve prompt! async function handler -->
- [x] #54606 <!-- fix double-counting and non-deterministic results in
`summarysize` -->
- [x] #54759 <!-- REPL: Fully populate the dummy Pkg prompt -->
- [x] #54702 <!-- lowering: Recognize argument destructuring inside
macro hygiene -->
- [x] #54678 <!-- Don't let setglobal! implicitly create bindings -->
- [x] #54730 <!-- Fix uuidkey of exts in fast path of `require_stdlib`
-->
- [x] #54765 <!-- Handle no-postdominator case in finalizer pass -->
- [x] #54591 <!-- Don't expose guard pages to malloc_stack API consumers
-->
- [x] #54755 <!-- [TOML] remove Dates hack, replace with explicit usage
-->
- [x] #54721 <!-- add try/catch around scheduler to reset sleep state
-->
- [x] #54631 <!-- Avoid concatenating LazyString in setindex! for
triangular matrices -->
- [x] #54322 <!-- effects: add new `@consistent_overlay` macro -->
- [x] #54785
- [x] #54865
- [x] #54815
- [x] #54795
- [x] #54779
- [x] #54837 

Contains multiple commits, manual intervention needed:
- [ ] #52694 <!-- Reinstate similar for AbstractQ for backward
compatibility -->
- [ ] #54649 <!-- Less restrictive copyto! signature for triangular
matrices -->

Non-merged PRs with backport label:
- [ ] #54779 <!-- make recommendation clearer on manifest version
mismatch -->
- [ ] #54739 <!-- finish implementation of upgradable stdlibs -->
- [ ] #54738 <!-- serialization: fix relocatability bug -->
- [ ] #54574 <!-- Make ScopedValues public -->
- [ ] #54457 <!-- Make `String(::Memory)` copy -->
- [ ] #53957 <!-- tweak how filtering is done for what packages should
be precompiled -->
- [ ] #53452 <!-- RFC: allow Tuple{Union{}}, returning Union{} -->
- [ ] #53286 <!-- Raise an error when using `include_dependency` with
non-existent file or directory -->
- [ ] #51479 <!-- prevent code loading from lookin in the versioned
environment when building Julia -->
@KristofferC KristofferC removed the backport 1.11 Change should be backported to release-1.11 label Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REPL Julia's REPL (Read Eval Print Loop)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants