livebook-dev / livebook

Automate code & data workflows with interactive Elixir notebooks

Home Page:https://livebook.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ImageComponent crashes livebook if passed non-binary `contents`

samrat opened this issue · comments

Environment

  • Elixir & Erlang/OTP versions (elixir --version): 1.15.7
  • Operating system: Erlang/OTP 26
  • How have you started Livebook (mix phx.server, livebook CLI, Docker, etc): macOS app
  • Livebook version (use git rev-parse HEAD if running with mix): v0.12.1

Logs

17:51:43.392 request_id=F8Mek8ZWKm2iaLwAJ2vh [error] GenServer #PID<0.31367.0> terminating
** (FunctionClauseError) no function clause matching in Base.encode64/2
    (elixir 1.15.7) lib/base.ex:393: Base.encode64(%{async: nil, private: %{}, status: 200, __struct__: Req.Response, body: <<137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 4, 0, 0, 0, 4, 0, 8, 2, 0, 0, 0, 240, 127, 188, 212, 0, 0, 57, 231, 99, 97, 66, 88, 0, 0, 57, 231, ...>>, headers: %{"accept-ranges" => ["bytes"], "content-disposition" => ["inline"], "content-md5" => ["F6r7QCXX4JlaH9Hk5nYmZA=="], "content-type" => ["image/png"], "date" => ["Thu, 04 Apr 2024 15:05:15 GMT"], "etag" => ["\"0x8DC54B87ED22AA0\""], "last-modified" => ["Thu, 04 Apr 2024 15:04:15 GMT"], "server" => ["Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0"], "x-ms-blob-type" => ["BlockBlob"], "x-ms-creation-time" => ["Thu, 04 Apr 2024 15:04:15 GMT"], "x-ms-lease-state" => ["available"], "x-ms-lease-status" => ["unlocked"], "x-ms-request-id" => ["4b8fa611-a01e-0062-4ea1-8686a8000000"], "x-ms-server-encrypted" => ["true"], "x-ms-version" => ["2021-08-06"]}, trailers: %{}}, [])
    (livebook 0.12.1) lib/livebook_web/live/output/image_component.ex:18: LivebookWeb.Output.ImageComponent.data_url/2
    (livebook 0.12.1) lib/livebook_web/live/output/image_component.ex:9: anonymous fn/2 in LivebookWeb.Output.ImageComponent.render/1
    (livebook 0.12.1) /Users/runner/work/livebook_cd/livebook_cd/lib/livebook_web/live/output.ex:71: LivebookWeb.Output.render_output/2
    (elixir 1.15.7) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 0.20.2) lib/phoenix_live_view/diff.ex:391: Phoenix.LiveView.Diff.traverse/7

Current behavior

  • Evaluate a cell with contents
contents =
  Req.get!(
    "https://picsum.photos/200/300"
  )

Kino.Image.new(contents, :png) # <-- incorrect, as Kino.Image expects binary but we passed Req.Response here

This causes Livebook to throw a 500 error, and the livemd file cannot be opened again inside Livebook.

This is happening because

image_base64 = Base.encode64(content)
passes content(which in this case is not a binary) to Base64.encode which always expects a binary.

Expected behavior

The Kino.Image.new should return an error without crashing the Livebook process.

Good catch, I added a guard in livebook-dev/kino@b50ed28 :)