Repository navigation
Conversation
…oll() poll() seeded its last value with poll_func(), which for an async poll_func is a coroutine that is never awaited. The polling effect then compared the first real value with that coroutine through equals, so a custom equals raised, the previous value was never replaced, and every later poll failed the same way. Seed an async poll_func with a placeholder and treat the first polled value as changed, without calling equals.
| last_value: reactive.Value[Any] = reactive.Value( | ||
| _NO_VALUE if _utils.is_async_callable(poll_func) else poll_func() | ||
| ) |
There was a problem hiding this comment.
I think this could make sense from a simplicity/consistency stance? I think this way we could also get rid of the with reactive.isolate()?
| last_value: reactive.Value[Any] = reactive.Value( | |
| _NO_VALUE if _utils.is_async_callable(poll_func) else poll_func() | |
| ) | |
| last_value: reactive.Value[Any] = reactive.Value(object()) |
There was a problem hiding this comment.
Agreed, done in f0119dd: the placeholder is now used for both sync and async poll_func, and the with reactive.isolate() is gone since poll_func is no longer called while seeding. One visible consequence: a sync poll_func is no longer called once at decoration time, so test_poll now expects one fewer poll_func call at each step; the value_func counts are unchanged. test_poll.py, test_otel_poll.py and test_reactives.py pass, and the CHANGELOG entry mentions the change.
… poll_func Review suggestion from cpsievert: one code path, and no reactive.isolate() needed because poll_func is no longer called while seeding.
|
Thank you for finding this and sending the fix, @pcbeingused333! The bug report and the failing test made the problem easy to follow. We'll push a small follow-up to this branch. Instead of a placeholder object, |
…ader Replace the placeholder object with an unset reactive.Value. Readers of the decorated function now wait for the first poll instead of running the expensive read before it and again after it. Add a test for a reader that runs before the first poll, and credit the author in the changelog.
…l errors Value.get() already raises a silent exception while unset and takes the dependency first, so the req(last_value.is_set()) calls did nothing. Move the equals() checks under an if block for the first poll. Add tests for a first poll that raises and for file_reader() with a missing file, and shorten the changelog entry.
deduplicate() read its function inside reactive.isolate() while the server function ran, which reached reactive.poll() before its first poll. Start the deduplicated value unset and let its effect set it. Also list the poll() change as a breaking change in the changelog.
reactive.poll()accepts anasyncpoll_func(the docstring says "Both regular functions and co-routine functions are allowed", and the polling effect awaits it). Butpoll()seeded its last value withreactive.Value(poll_func()), so for anasyncfunction that value was a coroutine object that is never awaited.The first run of the polling effect then called
equals(old, new)with that coroutine asold. With the default==this is just "not equal", but a customequalsthat reads the value, for examplelambda old, new: old["version"] == new["version"], raises.poll()turns that into its "couldn't be compared with a previously returned object"TypeErrorand stores it. Because the last value is only replaced after a successful comparison, every later poll fails the same way, and the data never loads.Change. For both
asyncand syncpoll_func, the last value now starts unset;poll_funcis not called when@poll()is applied. The polling effect reads the first value and stores it without callingequals.poll_functhe polling effect (priority=1by default) sets the value before outputs render, so outputs see data on their first render, as before. With anasyncpoll_functhat waits on I/O, outputs stay empty until it returns.poll_funcruns one fewer time per session, since the setup call is gone.test_pollexpects one fewerpoll_funccall at each step; thevalue_funccounts are unchanged.reactive.file_reader()with a missing file no longer raises when the decorator is applied. TheFileNotFoundErroris raised when the value is read, and the next poll recovers once the file exists.Breaking change. Reading a
@poll()or@file_reader()value outside the reactive runtime before the first poll, for example inreactive.isolate()while the server function runs, now raises a silent exception instead of returning the value. Reads from outputs, calcs, and effects wait for the first poll.CHANGELOG.mdlists this under breaking changes.The
model-scoreexample did this: itsdeduplicate()helper read a calc built on@poll()insidereactive.isolate()while the server function ran, so the first session after the server started never finished starting.deduplicate()now starts its value unset and lets its effect set it.Tests.
test_poll_async_poll_func_with_custom_equals: anasyncpoll_funcwith anequalsthat reads a key. Nothing is polled when@poll()runs, and the value can be read after the first poll and after the next interval.test_poll_reader_runs_before_first_poll(sync and async): a reader that runs before the polling effect waits for the first poll and doesn't make the read function run twice.test_poll_first_poll_raises: when the first poll raises, readers get that error, not a silent exception, and a later good poll recovers.test_file_reader_missing_file: the missing-file behavior above.This change was made with Claude Code.