Replace `lock(this)` with private lock object in `CompiledScriptBlockData` by euxaristia · Pull Request #27619 · PowerShell/PowerShell · GitHub
Skip to content

Replace lock(this) with private lock object in CompiledScriptBlockData#27619

Open
euxaristia wants to merge 1 commit into
PowerShell:masterfrom
euxaristia:fix/lock-this
Open

Replace lock(this) with private lock object in CompiledScriptBlockData#27619
euxaristia wants to merge 1 commit into
PowerShell:masterfrom
euxaristia:fix/lock-this

Conversation

@euxaristia

Copy link
Copy Markdown

PR Summary

Replace six instances of lock(this) with lock(_lock) using a dedicated private readonly object field to avoid synchronizing on the instance itself.

PR Context

Using this as a synchronization object is dangerous because external code could also lock on the same instance, leading to deadlocks or unexpected contention. This is a common threading anti-pattern flagged by static analysis.

PR Checklist

  • PR title is meaningful and in present tense, imperative mood
  • Changes are summarized in the PR description
  • All files have the Microsoft copyright header
  • Ready for review -- this is not a Draft PR
  • Breaking changes: None
  • User-facing changes: No
  • Testing: N/A -- refactoring only, no behavioral change

…Data`

Using `this` as a synchronization object is dangerous because external code could also lock on the same instance, leading to deadlocks or unexpected contention. Replace with a dedicated `_lock` field.
@euxaristia euxaristia requested a review from a team as a code owner June 21, 2026 22:07
Copilot AI review requested due to automatic review settings June 21, 2026 22:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces synchronization on the CompiledScriptBlockData instance (lock(this)) with a dedicated private lock object (lock(_lock)), reducing the risk of external code locking on the same instance and causing unexpected contention or deadlocks.

Changes:

  • Replaced six occurrences of lock(this) with lock(_lock) in CompiledScriptBlockData.
  • Introduced a private readonly lock field (_lock) to serve as the synchronization root for the class.

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.

2 participants