Skip to content

fix(reactive)!: don't call poll_func when @poll() is applied - #2576

Merged
schloerke merged 6 commits into
posit-dev:mainfrom
pcbeingused333:fixer3/fix/poll-async-initial-value
Nov 25, 2026
Merged

schloerke merged 6 commits into
posit-dev:mainfrom
pcbeingused333:fixer3/fix/poll-async-initial-value

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

reactive.poll() accepts an async poll_func (the docstring says "Both regular functions and co-routine functions are allowed", and the polling effect awaits it). But poll() seeded its last value with reactive.Value(poll_func()), so for an async function 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 as old. With the default == this is just "not equal", but a custom equals that reads the value, for example lambda old, new: old["version"] == new["version"], raises. poll() turns that into its "couldn't be compared with a previously returned object" TypeError and 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 async and sync poll_func, the last value now starts unset; poll_func is not called when @poll() is applied. The polling effect reads the first value and stores it without calling equals.

  • Until the first poll, reading the decorated function raises a silent exception, and its readers run again once the value is set. With a sync poll_func the polling effect (priority=1 by default) sets the value before outputs render, so outputs see data on their first render, as before. With an async poll_func that waits on I/O, outputs stay empty until it returns.
  • poll_func runs one fewer time per session, since the setup call is gone. test_poll expects one fewer poll_func call at each step; the value_func counts are unchanged.
  • reactive.file_reader() with a missing file no longer raises when the decorator is applied. The FileNotFoundError is 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 in reactive.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.md lists this under breaking changes.

The model-score example did this: its deduplicate() helper read a calc built on @poll() inside reactive.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: an async poll_func with an equals that 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.

…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.
Comment thread shiny/reactive/_poll.py Outdated
Comment on lines +108 to +110
last_value: reactive.Value[Any] = reactive.Value(
_NO_VALUE if _utils.is_async_callable(poll_func) else poll_func()
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()?

Suggested change
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())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@schloerke

Copy link
Copy Markdown
Collaborator

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, last_value will start unset, and the read function will req() it until the first poll. That way, an output that runs before the first poll waits for it, rather than reading the data twice. We'll also add a test for that case, and credit you in the changelog entry.

…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.
@schloerke
schloerke enabled auto-merge (squash) November 25, 2026 21:18
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.
@schloerke schloerke changed the title fix(reactive): don't call an async poll_func without awaiting it in poll() fix(reactive)!: don't call poll_func when @poll() is applied Nov 25, 2026
@schloerke
schloerke self-requested a review November 25, 2026 21:37
@schloerke
schloerke merged commit cbb4c6e into posit-dev:main Nov 25, 2026
178 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants