Is this locking correct in ModuleDefinition? #951

Open
opened 2024-06-19 17:54:25 -05:00 by omajid · 1 comment
omajid commented 2024-06-19 17:54:25 -05:00 (Migrated from github.com)

github.com/jbevain/cecil@8e1ae7b4ea/Mono.Cecil/ModuleDefinition.cs (L387-L393)

It seems like this sequence of events might be possible, resulting in creation of an DefaultAssemblyResolver instance that's never disposed:

  1. Thread 1 sees assembly_resolver.value == null
  2. Thread 2 sees assembly_resolver.value == null
  3. Thread 1 enters the lock block and sets assembly_resolver to a new instance
  4. Thread 2 enters the lock block and re-sets assembly_resolver to a new instance

The instance in step 3 is now lost and was never disposed.

https://github.com/jbevain/cecil/blob/8e1ae7b4ea67ccc38cb8db3ded6802643109ffd7/Mono.Cecil/ModuleDefinition.cs#L387-L393 It seems like this sequence of events might be possible, resulting in creation of an DefaultAssemblyResolver instance that's never disposed: 1. Thread 1 sees `assembly_resolver.value == null` 2. Thread 2 sees `assembly_resolver.value == null` 3. Thread 1 enters the `lock` block and sets `assembly_resolver` to a new instance 4. Thread 2 enters the `lock` block and re-sets `assembly_resolver` to a new instance The instance in step 3 is now lost and was never disposed.
ltrzesniewski commented 2024-06-20 10:07:14 -05:00 (Migrated from github.com)

I agree, using if/lock/if would be appropriate as it's a standard pattern, or just changing = to ??= in this case.

I agree, using `if`/`lock`/`if` would be appropriate as it's a standard pattern, or just changing `=` to `??=` in this case.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-jbevain/cecil#951
No description provided.