Skip to content

Conversation

Copy link

Copilot AI commented Nov 20, 2025

Fixes hashicorp#31371

Description

When Stop() is called before a Terraform Core operation starts (e.g., SIGINT during the 3-4 second window before state lock acquisition), the operation proceeds anyway. This is a race condition: Stop() checks for an active runContextCancel, finds none, and does nothing. When the operation subsequently calls acquireRun(), it creates a fresh context unaware of the cancellation.

Changes

Core fix: Create cancellation context at Context initialization rather than per-operation.

  • NewContext(): Initialize runContext and runContextCancel at construction time
  • Stop(): Always cancel the context (removed nil check that allowed race)
  • acquireRun(): Check if context already cancelled; abort operation immediately if so
  • releaseRun(): Remove context lifecycle management (context persists for Context lifetime)
  • Tests: Added TestContext2Plan_stopBeforeStart and TestContext2Apply_stopBeforeStart demonstrating fix

Target Release

1.15.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

No changes to security controls.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.
Original prompt

Please try to fix hashicorp#31371


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Create cancellation context at initialization time in NewContext instead of in acquireRun
- Check if context is already cancelled in acquireRun and abort early if so
- Simplify Stop() to always cancel the context
- Add tests demonstrating the fix works correctly

Co-authored-by: orgads <[email protected]>
Copilot AI changed the title [WIP] Fix issue 31371 in Terraform repository Fix early SIGINT not preventing subsequent operation execution Nov 20, 2025
Copilot AI requested a review from orgads November 20, 2025 07:20
Copilot finished work on behalf of orgads November 20, 2025 07:20
@orgads
Copy link
Owner

orgads commented Nov 20, 2025

This doesn't fix the issue. See hashicorp#37921 for a fix that works as expected.

@github-actions
Copy link

Changelog Warning

Currently this PR would target a v1.15 release. Please add a changelog entry for in the .changes/v1.15 folder, or discuss which release you'd like to target with your reviewer. If you believe this change does not need a changelog entry, please add the 'no-changelog-needed' label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Early SIGINT is not respected if a Terraform Core operation subsequently begins

2 participants