Skip to content

Listener Cannot Close Session - Missing Close Method Call #63

@coolguy1771

Description

@coolguy1771

The Listener.Run method currently leaks message sessions. Because the Client interface lacks a Close method, the listener exits—whether via context cancellation or error—without notifying the server. This results in "zombie" sessions and resource leaks on the scaleset API.

Proposed Changes

Update Client interface: Add Close(ctx context.Context) error to allow for graceful session termination.
Implement cleanup in Run: Use a defer block to ensure Close is called on exit.

Note: The cleanup uses context.Background() with a timeout because the primary ctx is likely already cancelled when the defer triggers.

type Client interface {
    GetMessage(ctx context.Context, lastMessageID, maxCapacity int) (*scaleset.RunnerScaleSetMessage, error)
    DeleteMessage(ctx context.Context, messageID int) error
    Session() scaleset.RunnerScaleSetSession
    Close(ctx context.Context) error // Added
}

func (l *Listener) Run(ctx context.Context, scaler Scaler) error {
    defer func() {
        // Ensure session cleanup even if parent context is cancelled
        cleanupCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
        defer cancel()
        
        if err := l.client.Close(cleanupCtx); err != nil {
            l.logger.Error("failed to close session", "error", err)
        }
    }()
    
    for {
        select {
        case <-ctx.Done():
            return ctx.Err()
        default:
            // ... processing logic ...
        }
    }
}

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