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

fix: non-deterministic random object-ids for session caching #3874

Merged
merged 1 commit into from
Feb 21, 2025
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
9 changes: 7 additions & 2 deletions frontend/src/core/dom/uiregistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { Logger } from "@/utils/Logger";
import type { CellId, UIElementId } from "../cells/ids";
import {
type ValueType,
Expand Down Expand Up @@ -134,7 +135,9 @@ export class UIElementRegistry {
buffers: string[] | undefined | null,
): void {
const entry = this.entries.get(objectId);
if (entry !== undefined) {
if (entry === undefined) {
Logger.warn("UIElementRegistry missing entry", objectId);
} else {
const base64ToDataView = (base64: string) => {
const bytes = window.atob(base64);
const buffer = new ArrayBuffer(bytes.length);
Expand Down Expand Up @@ -176,7 +179,9 @@ export class UIElementRegistry {
value: ValueType,
): void {
const entry = this.entries.get(objectId);
if (entry !== undefined) {
if (entry === undefined) {
Logger.warn("UIElementRegistry missing entry", objectId);
} else {
entry.value = value;
entry.elements.forEach((element) => {
if (element !== initiator) {
Expand Down
9 changes: 8 additions & 1 deletion marimo/_plugins/ui/_core/ui_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):
_value_frontend: S
_value: T

_random_seed = random.Random(42)
# We want this to be fully random in production,
# otherwise cached session state could use incorrect object-ids.
# And changing object-ids are a way to force a re-render.
#
# This does mean that snapshotting exports in CI will produce
# different object-ids. If this is a problem, we can allow a
# fixed seed via an environment variable.
_random_seed = random.Random()

def __init__(
self,
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
register_formatters()


@pytest.fixture(autouse=True)
def patch_random_seed(monkeypatch: pytest.MonkeyPatch) -> None:
"""Patch UIElement._random_seed to use a fixed seed for testing"""
import random

from marimo._plugins.ui._core.ui_element import UIElement

# Patch the random seed to be deterministic for testing
monkeypatch.setattr(UIElement, "_random_seed", random.Random(42))


@dataclasses.dataclass
class _MockStream(ThreadSafeStream):
"""Captures the ops sent through the stream"""
Expand Down
Loading