Skip to content

[Correctness] Service.cs — Restart-attempts file read outside _fileSemaphore, TOCTOU race #575

@Christophe-Rogiers

Description

@Christophe-Rogiers

Severity: Warning

File: src/Servy.Service/Service.cs
Lines: 849–853

Description:
ConditionalResetRestartAttemptsAsync checks the file counter via EnsureRestartAttemptsFileAsync() (which releases _fileSemaphore after), then directly calls File.Exists(_restartAttemptsFile) (line 851) and File.GetLastWriteTimeUtc(...) (line 853) outside any semaphore. Between the semaphore release and these bare file calls, another thread (e.g., a concurrent recovery) can modify the file, producing incorrect reset decisions.

Suggested fix:
Move the file-existence and GetLastWriteTimeUtc calls inside the _fileSemaphore scope, or read both the counter and the timestamp atomically in a single locked section.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions