For the complete documentation index, see llms.txt. This page is also available as Markdown.

Handling Unexpected Closure of Testing Executions

If a test framework fails or a pipeline stops early, some testing executions can remain open. These open executions can block coverage calculation and leave execution state inaccurate.

Most teams manage cleanup by environment. This is the most common pattern for functional testing, where one environment can host multiple related executions. This page focuses on that use case.

Use the Get Executions Status List API to:

  1. Query executions with status=created.

  2. Filter by labId.

  3. Optionally add testStage.

  4. Close each matching test session.

You can also manage cleanup by application version. This is common for unit tests and other in-process tests, where executions are tied to one specific app version. In that case, use bsid to target one specific build session for that version.

Details on the Get Executions Status List API are available in the API reference.

Below are sample implementations that query remaining open executions and close them one by one as part of post-run cleanup.

    post {
        aborted{
            echo '[Sealights] Cleanup executions left open.'
            withCredentials([string(credentialsId: 'sl.agent.token', variable: 'SL_AGENT_TOKEN')]) {
                sh '''
                    set +x
                    SL_DOMAIN="yourcustomdomain.sealights.co"
                    SL_AGENT_TOKEN=$(cat ./sl-agent-token.txt)

                    SL_LABID="MyLabID"
                    SL_TESTSTAGE="Functional Tests"

                    SL_TEST_EXECUTION_IDs=(`curl -sX GET "https://$SL_DOMAIN/sl-api/v1/executions?labId=$SL_LABID&testStage=$SL_TESTSTAGE&status=created" \
                                         -H "Authorization: Bearer $SL_AGENT_TOKEN" \
                                         -H "Content-Type: application/json" \
                                         | jq -r '.data.list | map(.executionId) | join(" ")'`)

                    # Optional: filter based on Test Stage and/or LabID

                    echo "Found ${#SL_TEST_EXECUTION_IDs[@]} executions"

                    for id in ${SL_TEST_EXECUTION_IDs[@]}
                    do 
                       echo -n "Closing Test Session ID $id: "
                       curl -isX DELETE "https://$SL_DOMAIN/sl-api/v1/test-sessions/$id" \
                         -H "Authorization: Bearer $SL_AGENT_TOKEN" \
                         -H "Content-Type: application/json" | grep HTTP
                    done
                '''
            }
        }
    }

GitHub Actions YAML executing the Sealights cleanup step when the Functional Tests job fails or is cancelled.

Python script

Best Practices

  • URL-encode testStage values that contain spaces or special characters before sending the request.

  • Verify the HTTP status for each DELETE request. Treat non-2xx responses as cleanup failures and log the execution ID for follow-up.

  • If the execution list spans more than one API response, iterate through every page before building SL_TEST_EXECUTION_IDs. Do not assume a busy shared lab fits in one response.

Last updated

Was this helpful?