Skip to content

Commit 82ec383

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 d6e91e1 commit 82ec383

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

player/javascript/defaults.js

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

667667
if (type == "complete" && result) {
668-
mp.commandv("script-message-to", "console", "complete",
669-
JSON.stringify(result[0]), result[1], result[2] || "");
668+
mp.commandv("script-message-to", "console", "complete", JSON.stringify({
669+
script_name: mp.script_name,
670+
handler_id: handler_id,
671+
list: result[0],
672+
start_pos: result[1],
673+
append: result[2] || "",
674+
}));
670675
}
671676
}
672677

player/lua/console.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,17 +1795,21 @@ mp.register_script_message("set-log", function (log)
17951795
render()
17961796
end)
17971797

1798-
mp.register_script_message("complete", function (list, start_pos, append)
1799-
if line ~= completion_old_line or cursor ~= completion_old_cursor then
1798+
mp.register_script_message("complete", function (message)
1799+
message = utils.parse_json(message)
1800+
1801+
if message.script_name ~= input_caller or message.handler_id ~= input_caller_handler then
1802+
return
1803+
elseif line ~= completion_old_line or cursor ~= completion_old_cursor then
18001804
return
18011805
end
18021806

18031807
completion_buffer = {}
18041808
selected_completion_index = 0
1805-
local completions = utils.parse_json(list)
1809+
local completions = message.list
18061810
table.sort(completions)
1807-
completion_pos = start_pos
1808-
completion_append = append
1811+
completion_pos = message.start_pos
1812+
completion_append = message.append
18091813
for i, match in ipairs(fuzzy_find(line:sub(completion_pos, cursor - 1),
18101814
completions)) do
18111815
completion_buffer[i] = completions[match[1]]

player/lua/input.lua

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

4343
if type == "complete" and completions then
44-
mp.commandv("script-message-to", "console", "complete",
45-
utils.format_json(completions), completion_pos,
46-
completion_append or "")
44+
mp.commandv("script-message-to", "console", "complete", utils.format_json({
45+
script_name = mp.get_script_name(),
46+
handler_id = handler_id,
47+
list = completions,
48+
start_pos = completion_pos,
49+
append = completion_append or "",
50+
}))
4751
end
4852
end
4953

0 commit comments

Comments
 (0)