Skip to content

Commit 35047bc

Browse files
mp.input: avoid rare race condition when supplying completions
It was previously possible for completion messages to be received by a new input request, if one was created while the message was in transit. Since it is trivial to avoid this by passing the script name and existing handle_id value, we may as well do so and guarantee that there will not be any data races.
1 parent 3b8a5df commit 35047bc

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

player/javascript/defaults.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,13 @@ function register_event_handler(t) {
669669
var result = t[type].apply(null, args);
670670

671671
if (type == "complete" && result) {
672-
mp.commandv("script-message-to", "console", "complete",
673-
JSON.stringify(result[0]), result[1], result[2] || "");
672+
mp.commandv("script-message-to", "console", "complete", JSON.stringify({
673+
script_name: mp.script_name,
674+
handler_id: handler_id,
675+
list: result[0],
676+
start_pos: result[1],
677+
append: result[2] || "",
678+
}));
674679
}
675680
}
676681

player/lua/console.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,17 +1792,21 @@ mp.register_script_message("set-log", function (log)
17921792
render()
17931793
end)
17941794

1795-
mp.register_script_message("complete", function (list, start_pos, append)
1796-
if line ~= completion_old_line or cursor ~= completion_old_cursor then
1795+
mp.register_script_message("complete", function (message)
1796+
message = utils.parse_json(message)
1797+
1798+
if message.script_name ~= input_caller or message.handler_id ~= input_caller_handler then
1799+
return
1800+
elseif line ~= completion_old_line or cursor ~= completion_old_cursor then
17971801
return
17981802
end
17991803

18001804
completion_buffer = {}
18011805
selected_completion_index = 0
1802-
local completions = utils.parse_json(list)
1806+
local completions = message.list
18031807
table.sort(completions)
1804-
completion_pos = start_pos
1805-
completion_append = append
1808+
completion_pos = message.start_pos
1809+
completion_append = message.append
18061810
for i, match in ipairs(fuzzy_find(line:sub(completion_pos, cursor - 1),
18071811
completions)) do
18081812
completion_buffer[i] = completions[match[1]]

player/lua/input.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ local function register_event_handler(t)
4848
t[type](unpack(utils.parse_json(args or "") or {}))
4949

5050
if type == "complete" and completions then
51-
mp.commandv("script-message-to", "console", "complete",
52-
utils.format_json(completions), completion_pos,
53-
completion_append or "")
51+
mp.commandv("script-message-to", "console", "complete", utils.format_json({
52+
script_name = mp.get_script_name(),
53+
handler_id = handler_id,
54+
list = completions,
55+
start_pos = completion_pos,
56+
append = completion_append or "",
57+
}))
5458
end
5559
end
5660

0 commit comments

Comments
 (0)