Skip to content

ToChannel observes a failed read; simplifications - #307

Merged
electricessence merged 5 commits into
masterfrom
refactor/pass5
Dec 3, 2026
Merged

electricessence merged 5 commits into
masterfrom
refactor/pass5

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

One bug fix, and simplifications from a look at the code added since rc.16. No public or protected signature changes (the PublicAPI files are untouched) and the generated text is byte-identical.

The bug

ToChannel.Produce started the read as a task and, for the forms whose caller discards the count (ToChannelReader, ToChannel(...), out _), dropped it unobserved. A provider failure while reading then raised TaskScheduler.UnobservedTaskException when the task was finalized, in addition to completing the channel with the error. Produce now attaches an OnlyOnFaulted continuation that reads the task's exception, the way the Dataflow pump already does; readCount stays the original task, so a caller that awaits it still gets the exception. A Fixed: line is in the release notes.

Test: ToChannelUnobservedTests, over ToChannelReader, ToChannel and ToChannel(out _). Without the fix it fails on the last assertion with Assert.Empty() Failure: Collection was not empty, holding three AggregateExceptions ("A Task's exception(s) were not observed ... The provider failed while reading."); with it the test passes. The test is serialized in the heap-measurement collection so no parallel test raises the same text into its handler, and it cannot pass vacuously: a faulted task the test drops itself must be reported unobserved, and the task of the third form (held only through a weak reference) must be dead, before the list is asserted empty.

Simplifications

  • The row reader's guard builds its wrapper exception with its constructor, or calls the four-argument RowErrors.Create with row -1, so the three-argument Create and RowMappingFailure.Defer, which only existed to be found by reflection, are deleted.
  • The reader refusal of a captured run puts only the call that can throw inside the try.
  • The name check of a parameter list is three case ... when arms instead of three nested ifs; the three-argument ResolveDerived, which had one caller, is deleted.
  • GenericPool no longer repeats Take as an explicit interface member; the public one implements both.
  • The asynchronous void ExecuteTransactionAsync calls the conditional core directly instead of going through the generic overload that wrapped the action a second time: one async state machine and one wrapping lambda fewer per transaction.
  • ExecuteReaderAsync on an IDbCommand that is a DbCommand passes the handler with a delegate variance cast instead of a wrapping lambda. The cast is an allocation saving, not a fix: the original lambda already bound the DbCommand overload (the new test covers both call sites).
  • DirectStream drops its looping Exchange helper for Interlocked.Exchange on dispose (the timestamp bits are read only while the state is idle, so dropping them in the terminal state is unobservable).
  • Generator: DeclaresNullPolicy no longer looks at fields (the two attributes cannot be put on one), and HasAttribute is one Any call. Generated text is byte-identical: the generator tests' output (counters on and off) and the generated benchmarks (net10.0 and net11.0) were compared before and after, file against file.

Left out

Three items overlap the wave of edits in #306 and are redone after it merges; one (a record struct for Lease.Taken) was dropped because the netstandard targets have no IsExternalInit.

Cost

Nothing per row. Per call: one wrapping lambda and one async state machine fewer on the transaction path, one closure fewer on ExecuteReaderAsync; the bug fix adds one continuation per ToChannel read.

Reviews

Two Haiku lenses (correctness including the fix and the test; rules, tests and the release note) read the diff. Applied: the test serialized and guarded as above, the refusal's try restored as before, byte order marks restored, argument grouping, a double blank line, and an xUnit analyzer note. Not applied: a release note for the variance cast (it fixes nothing).

Checks: Release 16,153 and Debug 16,132 tests pass (16 skipped), 189 generator tests, 0 build warnings on every target framework, 267 sample checks, dotnet format style and analyzers clean on all 28 checks, DocFX 0 warnings, the style scan clean.

🤖 Generated with Claude Code

electricessence and others added 5 commits December 2, 2026 17:17
…ported as unobserved

A failure in the read reaches the channel through Complete, as documented.
The task that ran the read was dropped by every form that does not return
the count (ToChannelReader, ToChannel, and ToChannel with an ignored out
argument), so when the task was collected, TaskScheduler.UnobservedTaskException
fired with the same failure. Produce now attaches an OnlyOnFaulted
continuation that reads the task's Exception, as the Dataflow pump does, and
still returns the original task: a caller who awaits the count sees the fault.

Cost: one continuation per ToChannel call, nothing per row.

Test: a failing read through the three forms that drop the count, then
GC.Collect and WaitForPendingFinalizers, asserts that no unobserved exception
names the read's failure. It fails before the change (three unobserved
exceptions) and passes after.

Release notes: one Fixed line under Changes since rc.16.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…er call

Each change keeps the behavior; no public or protected signature changes.

- The row reader's guard builds the wrapper exception with its constructor
  (a row-reading loop catches it) or calls the four-argument RowErrors.Create
  with row -1, so the three-argument Create and RowMappingFailure.Defer, which
  only existed to be found by reflection, are deleted.
- The reader refusal of a captured run puts only the call that can throw
  inside the try.
- The name check of a parameter list is three `case ... when` arms instead of
  three nested ifs, and the three-argument ResolveDerived, which had one
  caller, is deleted: that caller asks the four-argument one with `out _`.
- GenericPool no longer repeats Take as an explicit interface member: the
  public Take already implements both interfaces.
- The asynchronous void ExecuteTransactionAsync calls the conditional core
  directly instead of going through the generic overload, which wrapped the
  action a second time. The generic overload's async state machine and one
  wrapping lambda are gone per transaction.
- ExecuteReaderAsync on an IDbCommand that is a DbCommand passes the handler
  with a delegate variance cast instead of a wrapping lambda. The cast makes
  the DbCommand overload the only applicable one, so it cannot pick itself.
  One closure and one delegate fewer per call.
- DirectStream disposal sets the state with one Interlocked.Exchange instead
  of a compare-exchange loop; the timestamp the loop kept is only read while
  the state is Idle.

Cost: nothing is added on any path; the two overload changes remove an
allocation per call.

Tests: ExecuteReaderAsync on a DbCommand held as IDbCommand calls the handler
once for both return shapes (a wrong overload pick would recurse forever).
Core tests and generator tests pass in Release; whole solution builds with
0 warnings.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
DeclaresNullPolicy no longer looks at fields: NullPolicy and NullValue cannot
be put on a field, so a field never carried one in code that compiles.
HasAttribute is one Any call.

The generated text is byte-identical: the output of the generator tests
(counters on and off) and of the generated benchmarks (net10.0 and net11.0)
was compared before and after, file against file.

Cost: none at run time; the generator runs at build time.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…refusal's try as it was

- The unobserved-task test for ToChannel is its own class in the heap-measurement
  collection, which runs alone, so no parallel test raises the same text into its
  handler. It cannot pass vacuously: a faulted task it drops itself must be
  reported unobserved after the collection loop, and the task of the third form
  (held only through a weak reference) must be dead, before the list of the
  read's failures is asserted empty. Without the fix it still fails on that
  last assertion, with three unobserved exceptions.
- ReaderRefusal builds the refusal's exception inside the try again, as before
  the simplification, so the set of exceptions turned into a refusal is unchanged.
- Transaction.cs keeps its UTF-8 byte order mark, which the earlier commit dropped.
- The ExecuteReaderAsync test is named for what it checks (one call per call), its
  comment says what the cast does, and the cast has a one-line comment; the two
  arguments behavior and cancellationToken are grouped on one line.
- ModelReader lines are wrapped; a double blank line in DirectStream is removed.

No behavior change; the generated text is still byte-identical to the base.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The two new assertions named Assert.IsAssignableFrom, which the xUnit
analyzer reports (info); Assert.IsType with exactMatch false says the same.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit 5e01f85 into master Dec 3, 2026
9 checks passed
@electricessence
electricessence deleted the refactor/pass5 branch December 3, 2026 01:25
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.

1 participant