Skip to content

Commit ebab287

Browse files
authored
Thread safe readline (#669)
Block until other Reline.readline or Reline.readmultiline finish
1 parent 91b030c commit ebab287

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

lib/reline.rb

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class Core
7575

7676
def initialize
7777
self.output = STDOUT
78+
@mutex = Mutex.new
7879
@dialog_proc_list = {}
7980
yield self
8081
@completion_quote_character = nil
@@ -254,44 +255,48 @@ def get_screen_size
254255
Reline::DEFAULT_DIALOG_CONTEXT = Array.new
255256

256257
def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
257-
unless confirm_multiline_termination
258-
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
259-
end
258+
@mutex.synchronize do
259+
unless confirm_multiline_termination
260+
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
261+
end
260262

261-
Reline.update_iogate
262-
io_gate.with_raw_input do
263-
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
264-
end
263+
Reline.update_iogate
264+
io_gate.with_raw_input do
265+
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
266+
end
265267

266-
whole_buffer = line_editor.whole_buffer.dup
267-
whole_buffer.taint if RUBY_VERSION < '2.7'
268-
if add_hist and whole_buffer and whole_buffer.chomp("\n").size > 0
269-
Reline::HISTORY << whole_buffer
270-
end
268+
whole_buffer = line_editor.whole_buffer.dup
269+
whole_buffer.taint if RUBY_VERSION < '2.7'
270+
if add_hist and whole_buffer and whole_buffer.chomp("\n").size > 0
271+
Reline::HISTORY << whole_buffer
272+
end
271273

272-
if line_editor.eof?
273-
line_editor.reset_line
274-
# Return nil if the input is aborted by C-d.
275-
nil
276-
else
277-
whole_buffer
274+
if line_editor.eof?
275+
line_editor.reset_line
276+
# Return nil if the input is aborted by C-d.
277+
nil
278+
else
279+
whole_buffer
280+
end
278281
end
279282
end
280283

281284
def readline(prompt = '', add_hist = false)
282-
Reline.update_iogate
283-
io_gate.with_raw_input do
284-
inner_readline(prompt, add_hist, false)
285-
end
285+
@mutex.synchronize do
286+
Reline.update_iogate
287+
io_gate.with_raw_input do
288+
inner_readline(prompt, add_hist, false)
289+
end
286290

287-
line = line_editor.line.dup
288-
line.taint if RUBY_VERSION < '2.7'
289-
if add_hist and line and line.chomp("\n").size > 0
290-
Reline::HISTORY << line.chomp("\n")
291-
end
291+
line = line_editor.line.dup
292+
line.taint if RUBY_VERSION < '2.7'
293+
if add_hist and line and line.chomp("\n").size > 0
294+
Reline::HISTORY << line.chomp("\n")
295+
end
292296

293-
line_editor.reset_line if line_editor.line.nil?
294-
line
297+
line_editor.reset_line if line_editor.line.nil?
298+
line
299+
end
295300
end
296301

297302
private def inner_readline(prompt, add_hist, multiline, &confirm_multiline_termination)

test/reline/yamatanooroti/test_rendering.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,23 @@ def test_exit_with_ctrl_d
16901690
EOC
16911691
end
16921692

1693+
def test_thread_safe
1694+
start_terminal(6, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.')
1695+
write("[Thread.new{Reline.readline'>'},Thread.new{Reline.readmultiline('>'){true}}].map(&:join).size\n")
1696+
write("exit\n")
1697+
write("exit\n")
1698+
write("42\n")
1699+
close
1700+
assert_screen(<<~EOC)
1701+
>exit
1702+
>exit
1703+
=> 2
1704+
prompt> 42
1705+
=> 42
1706+
prompt>
1707+
EOC
1708+
end
1709+
16931710
def write_inputrc(content)
16941711
File.open(@inputrc_file, 'w') do |f|
16951712
f.write content

0 commit comments

Comments
 (0)