Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 19, 2025

The application was experiencing flaky test failures with segmentation fault panics in the GRPC Delete method and related operations. The panic occurred when tests started before the Raft cluster was fully initialized:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x15fa6d9]

goroutine 59200 [running]:
github.com/bootjp/elastickv/adapter.GRPCServer.Delete(...)
	/home/runner/work/elastickv/elastickv/adapter/grpc.go:166 +0x479

Root Cause

When the Raft cluster is not ready and coordinator.Dispatch() fails, it can return (nil, error). However, the error handling code was attempting to access res.CommitIndex on the nil result:

res, err := r.coordinator.Dispatch(reqs)
if err != nil {
    return &pb.DeleteResponse{
        CommitIndex: res.CommitIndex,  // ← panic here when res is nil
    }, errors.WithStack(err)
}

This affected four GRPC methods: RawPut, RawDelete, Put, and Delete.

Solution

Fixed the error handling to always use uint64(0) for CommitIndex when errors occur, which is semantically correct since failed operations should not report a commit index:

res, err := r.coordinator.Dispatch(reqs)
if err != nil {
    return &pb.DeleteResponse{
        CommitIndex: uint64(0),  // Always use 0 for failed operations
        Success:     false,
    }, errors.WithStack(err)
}

Testing

  • Verified the code builds successfully with the changes
  • Confirmed that the fix prevents nil pointer dereference panics
  • Tested that error responses now consistently return CommitIndex: 0 when the coordinator fails to dispatch requests

This fix makes the system more robust during cluster initialization and eliminates the flaky test failures caused by timing issues with Raft cluster readiness.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] 次のようなエラーがでてflaky testが発生しています。 おそらくクラスタの準備ができる前にテストが始まることが原因だと考えていますが、今一度コードを確認し調査してください Run go t... Fix nil pointer dereference panic in GRPC server methods during cluster initialization Aug 19, 2025
Copilot AI requested a review from bootjp August 19, 2025 19:26
adapter/grpc.go Outdated

res, err := r.coordinator.Dispatch(m)
if err != nil {
commitIndex := uint64(0)
Copy link
Owner

Choose a reason for hiding this comment

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

エラーのときは常に uint64(0) で良いのではないですか?

Copy link
Owner

Choose a reason for hiding this comment

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

@copilot

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right! Simplified the error handling to always use uint64(0) for CommitIndex when there's an error, which is cleaner and more semantically correct. Applied to all four methods: RawPut, RawDelete, Put, and Delete. Commit: d056645

Copilot AI requested a review from bootjp August 20, 2025 05:40
Copy link
Owner

@bootjp bootjp left a comment

Choose a reason for hiding this comment

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

LGTM

@bootjp bootjp marked this pull request as ready for review August 20, 2025 05:59
@bootjp bootjp enabled auto-merge August 20, 2025 05:59
@bootjp bootjp merged commit 0e1189e into main Aug 22, 2025
6 checks passed
@bootjp bootjp deleted the copilot/fix-dc945383-5925-4725-a58d-6e2e63e685ed branch August 22, 2025 11:50
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