Skip to content

Commit

Permalink
Raise an error when trying to read input value from a background process
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Jun 28, 2024
1 parent 467bb05 commit 2373643
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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

0 comments on commit 2373643

Please sign in to comment.