Skip to content

ProtectedKeyProvider: no retry on File.ReadAllBytes — AV lock fails service startup #255

@Christophe-Rogiers

Description

@Christophe-Rogiers

Description

ProtectedKeyProvider.cs line 118 reads the encryption key file with File.ReadAllBytes() without retry logic. On Windows, antivirus or backup software can briefly lock files.

Code

encrypted = File.ReadAllBytes(path);  // throws immediately on lock

Scenario

Windows Defender or backup agent momentarily locks the key file during a scan → IOException → service fails to start → requires manual restart after the lock clears.

Suggested Fix

Retry with short exponential backoff:

const int maxRetries = 3;
for (int attempt = 0; ; attempt++)
{
    try
    {
        encrypted = File.ReadAllBytes(path);
        break;
    }
    catch (IOException) when (attempt < maxRetries - 1)
    {
        Thread.Sleep(100 * (attempt + 1));
    }
}

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