From 2373643076db0210338ad396dccdc0af30cc82f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Fri, 28 Jun 2024 22:27:19 +0700 Subject: [PATCH] Raise an error when trying to read input value from a background process --- lib/kino/bridge.ex | 13 +++++++++++-- lib/kino/input.ex | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/kino/bridge.ex b/lib/kino/bridge.ex index d0d9f804..1e64856c 100644 --- a/lib/kino/bridge.ex +++ b/lib/kino/bridge.ex @@ -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 """ diff --git a/lib/kino/input.ex b/lib/kino/input.ex index 1731454a..93116520 100644 --- a/lib/kino/input.ex +++ b/lib/kino/input.ex @@ -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