Prevent script dump on job cancellation through UI due to race condition

Summary

A GitLab Dedicated customer (internal ZD link) reported that they observed a job which had been manually cancelled by a user generating debug output including variable values, without CI_DEBUG_TRACE being enabled. Original request from an internal request: https://gitlab.com/gitlab-com/request-for-help/-/issues/3236.

The behavior is similar to the issue that this MR !4980 (merged) was trying to fix.

Steps to reproduce

Not reproducible by customer and support.

.gitlab-ci.yml
Add the job definition that is failing here

Actual behavior

Intermittently, jobs get logged with script dump containing expanded variables.

Expected behavior

No script dump in job logs if not configured to do so.

Relevant logs and/or screenshots

This is the job log that was produced, showing the output from the build job, the script canceled externally message, the Cleaning up project directory message, and then the appearance of the unexpected debug logs. See internal issue section on expanded logs.

Environment description

Customer was on GitLab Dedicated 18.1

See internal issue section on pipeline configuration.

Used GitLab Runner version

Self-hosted v17.10.1 Kubernetes executor

Possible fixes

@ggeorgiev_gitlab noted

@jfarmiloe I belive the fix in Prevent script dump on job cancellation through UI (gitlab-org/gitlab-runner!4980 - merged) - Romuald Atchadé - OOO until Nov 30th, 2025 - 17.5 did not fully fix this.

We might have to take a step back as it appears that the trap isn't always catching the output due to a race condition between the eval and the trap.

Edited by Daphne Kua