From 82d748a1ffaef4f8d460b89e084a39bd8565d0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 22 Dec 2022 11:38:43 +0100 Subject: [PATCH] Don't track :rand and :random keys in process dictionary (#1599) --- lib/livebook/runtime/evaluator.ex | 2 +- test/livebook/runtime/evaluator_test.exs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/livebook/runtime/evaluator.ex b/lib/livebook/runtime/evaluator.ex index a09546589..1b0c2197d 100644 --- a/lib/livebook/runtime/evaluator.ex +++ b/lib/livebook/runtime/evaluator.ex @@ -293,7 +293,7 @@ defmodule Livebook.Runtime.Evaluator do Process.put(@ebin_path_key, ebin_path) - ignored_pdict_keys = Process.get_keys() |> MapSet.new() + ignored_pdict_keys = MapSet.new([:rand_seed, :random_seed] ++ Process.get_keys()) state = %{ evaluator_ref: evaluator_ref, diff --git a/test/livebook/runtime/evaluator_test.exs b/test/livebook/runtime/evaluator_test.exs index 11eb7113e..72a99ef74 100644 --- a/test/livebook/runtime/evaluator_test.exs +++ b/test/livebook/runtime/evaluator_test.exs @@ -96,6 +96,28 @@ defmodule Livebook.Runtime.EvaluatorTest do assert_receive {:runtime_evaluation_response, :code_3, {:ok, 2}, metadata()} end + test "keeps :rand state intact in process dictionary", %{evaluator: evaluator} do + Evaluator.evaluate_code(evaluator, ":rand.seed(:default, 0)", :code_1, []) + assert_receive {:runtime_evaluation_response, :code_1, _, metadata()} + + Evaluator.evaluate_code(evaluator, ":rand.uniform()", :code_2, []) + assert_receive {:runtime_evaluation_response, :code_2, {:ok, number1}, metadata()} + + Evaluator.evaluate_code(evaluator, ":rand.uniform()", :code_2, []) + assert_receive {:runtime_evaluation_response, :code_2, {:ok, number2}, metadata()} + + assert number1 != number2 + + Evaluator.evaluate_code(evaluator, ":rand.seed(:default, 0)", :code_1, []) + assert_receive {:runtime_evaluation_response, :code_1, _, metadata()} + + Evaluator.evaluate_code(evaluator, ":rand.uniform()", :code_2, []) + assert_receive {:runtime_evaluation_response, :code_2, {:ok, ^number1}, metadata()} + + Evaluator.evaluate_code(evaluator, ":rand.uniform()", :code_2, []) + assert_receive {:runtime_evaluation_response, :code_2, {:ok, ^number2}, metadata()} + end + test "captures standard output and sends it to the caller", %{evaluator: evaluator} do Evaluator.evaluate_code(evaluator, ~s{IO.puts("hey")}, :code_1, [])