Skip to content

Race condition in FileBodyStore #393

@gingters

Description

@gingters

We get an unhandled(!) exception, when the file body store tries to remove a response body file, while this file / stream is still in use (i.e. framework did not yet dispose the stream after sending out the content to the client).

Seems to be a race condition.

Proposed solution:
a) handle the exception. We should not have this unhandled.
b) if we get the IOException, log this as INFO only. Then try to Task.Delay by a few seconds and retry the deletion. If that also fails, then log as ERROR, but still have it handled. In this case we end up with a file that might clog the file storage, but at least we have logged this.

Here the exception with stack trace:

System.IO.IOException: The process cannot access the file 'C:\Users\SebastianGingter\AppData\Local\Temp\res_c6cc96b9-864c-46f8-837d-4ecc105b4886' because it is being used by another process.
   at System.IO.FileSystem.DeleteFile(String fullPath)
   at System.IO.File.Delete(String path)
   at Thinktecture.Relay.Server.Transport.FileBodyStore.RemoveResponseBodyAsync(Guid requestId, CancellationToken cancellationToken) in C:\Dev\tt\relayserver3\src\Thinktecture.Relay.Server\Transport\FileBodyStore.cs:line 142
   at Thinktecture.Relay.Server.Transport.FileBodyStore.<>c__DisplayClass13_0.<GetResponseBodyRemoveDisposable>b__0() in C:\Dev\tt\relayserver3\src\Thinktecture.Relay.Server\Transport\FileBodyStore.cs:line 158
   at Thinktecture.Relay.Server.DisposeAction.DisposeAsync() in C:\Dev\tt\relayserver3\src\Thinktecture.Relay.Server.Abstractions\DisposeAction.cs:line 22
   at Thinktecture.Relay.Server.Middleware.RelayContext`2.DisposeAsync() in C:\Dev\tt\relayserver3\src\Thinktecture.Relay.Server\Middleware\RelayContext.cs:line 68
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.<DisposeAsync>g__Await|22_0(Int32 i, ValueTask vt, List`1 toDispose)
   at Microsoft.AspNetCore.Http.Features.RequestServicesFeature.<DisposeAsync>g__Awaited|9_0(RequestServicesFeature servicesFeature, ValueTask vt)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<FireOnCompleted>g__ProcessEvents|227_0(HttpProtocol protocol, Stack`1 events)

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions