Skip to content

Code smell: Missing timeout configuration for command execution #1485

@thomhurst

Description

@thomhurst

Location

  • src/ModularPipelines/Context/Command.cs (lines 259-348)
  • src/ModularPipelines/Options/CommandLineOptions.cs

Problem

The command execution in Command.cs does not support configurable timeouts. While there is a hardcoded 30-second "forceful cancellation" timeout after the CancellationToken is triggered (line 279):

cancellationToken.Register(() =>
{
    try
    {
        if (forcefulCancellationToken.Token.CanBeCanceled)
        {
            forcefulCancellationToken.CancelAfter(TimeSpan.FromSeconds(30));
        }
    }
    ...
});

There is no way for users to:

  1. Set an overall timeout for a command execution
  2. Configure how long to wait before forcefully terminating a process
  3. Prevent runaway processes from consuming resources indefinitely

Impact

  • Long-running or hung commands can block pipeline execution indefinitely
  • No way to enforce SLAs or time limits on command execution
  • Pipeline may hang if external tools become unresponsive
  • Resource exhaustion from zombie processes

Suggested Fix

Add a Timeout property to CommandLineOptions:

public record CommandLineOptions
{
    // ... existing properties ...
    
    /// <summary>
    /// Maximum time to wait for the command to complete.
    /// Null means no timeout (wait indefinitely).
    /// </summary>
    public TimeSpan? Timeout { get; init; }
    
    /// <summary>
    /// Time to wait for graceful shutdown before forceful termination.
    /// Default is 30 seconds.
    /// </summary>
    public TimeSpan GracefulShutdownTimeout { get; init; } = TimeSpan.FromSeconds(30);
}

Then use these values in Command.cs:

using var timeoutCts = options.Timeout.HasValue 
    ? new CancellationTokenSource(options.Timeout.Value) 
    : new CancellationTokenSource();
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions