Skip to content

MessageSessionClient.Close can panic on nil session #57

@coolguy1771

Description

@coolguy1771

Bug

Close() currently panics if called when a session hasn't been established (e.g., if session creation failed during initialization). The method attempts to access c.session.SessionID without verifying that c.session is non-nil.

Fix

Added a guard clause to return an error instead of panicking when the session is nil.

func (c *MessageSessionClient) Close(ctx context.Context) error {
    c.mu.Lock()
    defer c.mu.Unlock()
    
    if c.session == nil {
        return fmt.Errorf("cannot close: no active session found")
    }
    
    return c.deleteMessageSession(ctx, c.scaleSetID, c.session.SessionID)
}

Test Case

Verified the fix with the following unit test:

func TestMessageSessionClient_Close_NilSession(t *testing.T) {
    client := &MessageSessionClient{session: nil}
    err := client.Close(context.Background())
    
    if err == nil {
        t.Fatal("expected error when closing nil session, got nil")
    }
    if !strings.Contains(err.Error(), "no active session") {
        t.Errorf("unexpected error message: %v", err)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions