Skip to content

Batch 3 Bugfix: Fix NullReferenceException in ByteStreamReader.WriteToFile default args#252

Draft
MaxHeimbrock wants to merge 1 commit intomainfrom
max/fix-write-to-file-null-safety
Draft

Batch 3 Bugfix: Fix NullReferenceException in ByteStreamReader.WriteToFile default args#252
MaxHeimbrock wants to merge 1 commit intomainfrom
max/fix-write-to-file-null-safety

Conversation

@MaxHeimbrock
Copy link
Copy Markdown
Contributor

Summary

`ByteStreamReader.WriteToFile(string directory = null, string nameOverride = null)` threw `ArgumentNullException` from the generated protobuf setter when either argument was null — which is exactly the documented default:

```
System.ArgumentNullException: Value cannot be null. Parameter name: value
at Google.Protobuf.ProtoPreconditions.CheckNotNull[T] (...)
at LiveKit.Proto.ByteStreamReaderWriteToFileRequest.set_NameOverride (...)
at LiveKit.ByteStreamReader.WriteToFile (...) in DataStream.cs:397
```

Same pattern the rest of the codebase already uses a null guard for (see `StreamTextOptions.ToProto`, `StreamByteOptions.ToProto` which each have a `// TODO: these fields are optional, but the generated proto is not allowing null values` comment and guard accordingly). The `WriteToFile` path just missed them.

The fix

```diff

  • writeToFileReq.Directory = directory;
  • writeToFileReq.NameOverride = nameOverride;
  • if (directory != null) writeToFileReq.Directory = directory;
  • if (nameOverride != null) writeToFileReq.NameOverride = nameOverride;
    ```

Validation

Ran a PlayMode E2E regression test locally (`reader.WriteToFile(outDir)` inside a byte-stream handler). Before the fix, the call threw synchronously. After, the call returns a `WriteToFileInstruction` and the handler proceeds normally.

Why no regression test in this PR

Any PlayMode test that creates a `ByteStreamReader` and releases it through normal scope exit trips CLT-2773: the `FfiHandle` `SafeHandle` finalizer calls `FfiDropHandle` off the Tokio runtime, aborting the Unity process on teardown. NUnit reports the test as passing before the abort, but the script's exit code captures the abort and reports "iteration failed." That's pre-existing, tracked separately, and explicitly out of scope for the current test-coverage workstream. Once CLT-2773 is resolved, a regression test can be added as a small follow-up.

Related

There are two other similar null-safety issues in this file that I noticed while in here — `ByteStreamWriter.Close(string reason = null)` and `TextStreamWriter.Close(string reason = null)` both pass `reason` directly to their proto setters and would throw on `Close()` without args. Leaving those for a follow-up to keep this PR focused.

🤖 Generated with Claude Code

ByteStreamReader.WriteToFile(string directory = null, string nameOverride = null)
threw ArgumentNullException from the generated protobuf setter when either
argument was null (which is the documented default). The two affected
assignments are the same pattern the rest of the codebase uses with a null
guard (see StreamTextOptions.ToProto, StreamByteOptions.ToProto, etc.);
the WriteToFile path simply missed them.

    writeToFileReq.Directory = directory;      // ArgumentNullException if null
    writeToFileReq.NameOverride = nameOverride; // ArgumentNullException if null

Adds null guards matching the existing codebase convention.

Validation: locally ran a PlayMode E2E regression test that called
reader.WriteToFile(outDir) from a byte-stream handler. Before this fix, the
call threw synchronously. After, the call returns a WriteToFileInstruction
and the handler proceeds normally. (The instruction's final completion is
the Rust side's job and is orthogonal to this C# null-safety fix.)

A persistent regression test is not added in this PR because any test that
creates a ByteStreamReader and releases it through normal scope exit trips
CLT-2773 (FfiHandle's SafeHandle finalizer calls FfiDropHandle off the
Tokio runtime, aborting the Unity process on teardown). NUnit reports the
test as passing before the abort, but the script's exit code captures the
abort. That's pre-existing, tracked separately, and out of scope here.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@MaxHeimbrock MaxHeimbrock changed the title Fix NullReferenceException in ByteStreamReader.WriteToFile default args Batch 3 Bugfix: Fix NullReferenceException in ByteStreamReader.WriteToFile default args Apr 20, 2026
@MaxHeimbrock MaxHeimbrock marked this pull request as draft April 27, 2026 09:49
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