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

Raise an error when trying to read input value from a background process #451

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/kino/bridge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,18 @@ defmodule Kino.Bridge do
Note that the input must be known to Livebook, otherwise
an error is returned.
"""
@spec get_input_value(String.t()) :: {:ok, term()} | {:error, :not_found} | request_error()
@spec get_input_value(String.t()) ::
{:ok, term()} | {:error, :not_found} | {:error, :bad_process} | request_error()
def get_input_value(input_id) do
with {:ok, reply} <- io_request({:livebook_get_input_value, input_id}), do: reply
pid = self()

io_request_result =
with {:request_error, :unsupported} <-
io_request({:livebook_get_input_value, input_id, pid}),
# Livebook <= v0.13.2 does not support the request with pid
do: io_request({:livebook_get_input_value, input_id})

with {:ok, reply} <- io_request_result, do: reply
end

@doc """
Expand Down
8 changes: 8 additions & 0 deletions lib/kino/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,14 @@ defmodule Kino.Input do
raise "failed to read input value, input not found." <>
" Make sure to render the input before reading its value"

{:error, :bad_process} ->
raise "input value can only be read in the main evaluation process," <>
" but Kino.Input.read/1 was called by another process." <>
" You can read the input value upfront and pass it to the process." <>
" In case you want to read the latest input value from a long-running" <>
" process, consider using Kino.Control.form/2, or subscribing to the" <>
" input change using one of the functions in the Kino.Control module"

{:request_error, reason} ->
raise "failed to read input value, reason: #{inspect(reason)}"
end
Expand Down
Loading