I'm working on an ASP.NET Core application running inside a Docker container. I have an API endpoint that processes a file asynchronously. The method picks a pending file, triggers an async validation process in a separate thread, and then immediately returns a response. However, when running inside Docker, I don't see any response after setting result.Data. The request just hangs.
below is the Sample code,
[Route("ProcessFile"), HttpPost]
public async Task<ApiResponse> ProcessFile(long id)
{
ApiResponse response = new ApiResponse();
List<FileLog> pendingFiles = _fileLogService.GetWhere(x => x.Status == 0).OrderBy(x => x.CreatedDate).ToList();
FileLog selectedFile = new FileLog();
try
{
if (pendingFiles != null && pendingFiles.Count > 0)
{
// Select the file matching the given ID
selectedFile = pendingFiles.FirstOrDefault(f => f.FileId.ToString().EndsWith(id.ToString()));
if (selectedFile != null)
{
List<FileLog> filesToProcess = new List<FileLog> { selectedFile };
Console.WriteLine("Starting async validation process...");
Task.Run(async () => await ValidateFileAsync(filesToProcess));
response.Message = $"File {selectedFile.FileName} (ID: {selectedFile.FileId}) picked for validation.";
response.Success = true;
}
else
{
response.Message = $"No pending file found with ID ending in {id}.";
response.Success = false;
}
}
else
{
response.Message = "No pending files available for validation.";
response.Success = false;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while processing file validation");
response.Message = $"Failed to process file with ID: {selectedFile.FileId}";
response.Success = false;
}
return response;
}
- The method works perfectly in UAT Docker but hangs in Production Docker after setting response.Message.
- No error logs are seen in the production pod logs.
- No exception is thrown in the API, and nothing is logged after response.Message.
- The ValidateFileAsync() method is supposed to run in the background, and the API should return immediately, but that doesn’t happen in Production.
Additional Context:
In Production, we trigger this API via a Task Scheduler running on an app server.
The Task Scheduler logs confirm that it receives this response:
result.Data = "The file - {selectedFile.FileName} having File ID: {selectedFile.FileId} is picked for processing validations."
But after this point, the method doesn't continue execution in Production.
Question:
- Why does this behavior occur only in Docker and not in my local environment?
- Could this be related to how Task.Run behaves in a containerized environment?
- Is there a better way to handle background processing in ASP.NET Core hosted in Docker?
await ValidateFileAsync(filesToProcess)with noTask.Run.