Skip to content

fix: add comma-ok guards for type assertion panics#30

Merged
ankurs merged 3 commits intomainfrom
fix/type-assertion-panic
Apr 4, 2026
Merged

fix: add comma-ok guards for type assertion panics#30
ankurs merged 3 commits intomainfrom
fix/type-assertion-panic

Conversation

@ankurs
Copy link
Copy Markdown
Member

@ankurs ankurs commented Apr 4, 2026

Summary

  • Add comma-ok guards to two unsafe type assertions in plan.go that could panic if reflect.Value.Interface().(error) fails
  • Defense-in-depth: IsValidBuilder validates at registration time, but reflection at runtime can still misbehave
  • Add TestPlanRun_ErrorFlowWithCommaOk to verify error propagation through the guarded path

Test plan

  • make test passes with -race
  • make lint clean
  • make doc regenerated README

Summary by CodeRabbit

  • Documentation

    • Added documentation for a new package-version constant and updated function reference links to correct source locations.
  • Bug Fixes

    • Improved runtime error handling to avoid crashes when components return unexpected types and to provide clearer error messages.
  • Tests

    • Added a unit test covering the error flow and verifying no goroutine leaks.

…cution

plan.go has two unsafe type assertions on reflect.Value outputs that could
panic if the second return value is not an error type. Although IsValidBuilder
validates at registration time, this adds defense-in-depth comma-ok guards
at both processWork (line 144) and doWorkAndGetResult (line 182).
Copilot AI review requested due to automatic review settings April 4, 2026 06:20
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 4, 2026

📝 Walkthrough

Walkthrough

Added a README constant and adjusted anchor links; introduced guarded runtime checks when interpreting builder function second return values to avoid panics; added a unit test verifying error propagation and no goroutine leaks.

Changes

Cohort / File(s) Summary
Documentation & Constants
README.md
Added documented exported constant SupportPackageIsVersion1 and updated function anchor links for BuildGraph, IsValidBuilder, MaxPlanParallelism, New, and Result.Get.
Worker / Error Handling
plan.go
Imported fmt and replaced unchecked outputs[1].Interface().(error) assertions with comma-ok-style type checks; when the second return is not an error, produce a formatted error instead of panicking in processWork and doWorkAndGetResult.
Tests
plan_test.go
Added TestPlanRun_ErrorFlowWithCommaOk to assert builder error flow yields an error and that no goroutine leaks occur (goleak.VerifyNone).

Sequence Diagram(s)

sequenceDiagram
    participant Plan as Plan
    participant Worker as Worker goroutine
    participant Builder as Builder function
    participant Span as Tracing Span
    participant Result as Result aggregator

    Plan->>Worker: schedule work item
    Worker->>Builder: invoke builder(...) -> (value, second)
    Builder-->>Worker: returns (value, second)
    Worker->>Span: set attributes
    alt second implements error
        Worker->>Span: set error = second
        Worker->>Result: record error
    else second not error
        Worker->>Span: set error = formatted type message
        Worker->>Result: record formatted error
    end
    Worker->>Plan: send completion
    Plan->>Result: aggregate outcomes
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 I hopped through code with careful paw,
Checked the second value — no more flaw.
Comma-ok makes panics cease to roar,
Errors now labeled, neat as before.
Tests and leeks (no leaks!) — hop for more. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: add comma-ok guards for type assertion panics' directly describes the main technical change: adding type assertion guards to prevent panics in plan.go.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/type-assertion-panic

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens runtime reflection error-handling in plan execution by avoiding potential panics from unsafe .(error) assertions, and adds a regression test around error propagation.

Changes:

  • Add comma-ok guards around reflected error extraction in processWork and doWorkAndGetResult.
  • Add a new plan run test to ensure error propagation remains correct.
  • Regenerate README docs (new “Constants” section + updated links).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
README.md Regenerated documentation and index to include constants and updated source links.
plan.go Adds guarded type assertions and fallback error creation when the second return isn’t an error.
plan_test.go Adds a test validating error propagation through the guarded path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread plan.go Outdated
Comment thread plan.go Outdated
Comment thread plan.go Outdated
Comment thread plan.go Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
plan_test.go (1)

97-110: This test doesn’t actually exercise the new non-error fallback branch.

DBTestFuncErr returns a proper error, so this validates existing error propagation, not the new "second return value is not an error" path. Consider either renaming the test for accuracy or adding a focused case that hits the mismatch branch directly.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@plan_test.go` around lines 97 - 110, The test
TestPlanRun_ErrorFlowWithCommaOk currently uses DBTestFuncErr which returns a
real error and therefore doesn't exercise the new "second return value is not an
error" fallback; either rename the test to reflect that it checks normal error
propagation or add a focused test that triggers the mismatch branch by
registering a builder that returns a non-error second value (e.g., create
DBTestFuncSecondNotError that returns (someResult, "not-an-error") or another
non-error type), compile the plan and call executionPlan.Run(...) expecting an
error that contains the mismatch message (e.g., "second return value is not an
error"), and keep references to processWork and doWorkAndGetResult in the test
description so reviewers can locate the code paths being exercised.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@plan_test.go`:
- Around line 97-110: The test TestPlanRun_ErrorFlowWithCommaOk currently uses
DBTestFuncErr which returns a real error and therefore doesn't exercise the new
"second return value is not an error" fallback; either rename the test to
reflect that it checks normal error propagation or add a focused test that
triggers the mismatch branch by registering a builder that returns a non-error
second value (e.g., create DBTestFuncSecondNotError that returns (someResult,
"not-an-error") or another non-error type), compile the plan and call
executionPlan.Run(...) expecting an error that contains the mismatch message
(e.g., "second return value is not an error"), and keep references to
processWork and doWorkAndGetResult in the test description so reviewers can
locate the code paths being exercised.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 91470986-5ad6-4afd-b01a-f1e4a5af87ee

📥 Commits

Reviewing files that changed from the base of the PR and between b8d8f57 and c38ddeb.

📒 Files selected for processing (3)
  • README.md
  • plan.go
  • plan_test.go

Address review feedback:
- Use type-only (%T) instead of value (%v) to avoid leaking sensitive
  data or bloating output in the defense-in-depth error path
- Store Interface() result once to avoid double call
- Fix indentation in doWorkAndGetResult comma-ok block
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread plan.go Outdated
Comment thread plan_test.go Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@plan.go`:
- Around line 145-150: The code calls reflect.Value.IsNil() on the second return
value (secondReturn := o.outputs[1].Interface()) without checking the value's
Kind, which can panic for non-nilable kinds; add a small helper (e.g.,
safeIsNil(v interface{}) bool) that returns true if v is nil or v is a nil-able
kind (Chan, Func, Interface, Map, Ptr, Slice) and its reflect.Value.IsNil() is
true, otherwise returns false; replace the inline IsNil checks around
o.outputs[1].Interface() and the other occurrence with calls to this helper and
use reflect.ValueOf(v).IsValid() / nil check as appropriate before calling IsNil
to avoid panics while preserving the existing error-logging behavior
(span.SetError(fmt.Errorf(...)) / span.SetError(errVal)).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a86330e9-30d6-421f-bcfc-bad8095d7bd6

📥 Commits

Reviewing files that changed from the base of the PR and between 22f5983 and 7fe9c72.

📒 Files selected for processing (2)
  • plan.go
  • plan_test.go
✅ Files skipped from review due to trivial changes (1)
  • plan_test.go

Comment thread plan.go
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ankurs ankurs merged commit 2faa7ed into main Apr 4, 2026
12 checks passed
@ankurs ankurs deleted the fix/type-assertion-panic branch April 4, 2026 09:03
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.

2 participants