You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure文件共享挂载AKS时是否会引发‘文件正被占用’IOException?

问题:Azure File Share 挂载后写入文件出现进程占用异常,吞吐量下降

我正在开发一项服务,流程为从Azure Service Bus Queue获取消息,经简单处理/验证后,将消息内容保存到文件,元数据存入数据库。服务运行在AKS上(基于dotnet 6 Docker镜像),挂载了通过Azure Disk CSI连接的Azure File Share。消息量可达每分钟数万条,服务持续运行处理。近期测试中偶尔出现大量System.IO.IOException,导致吞吐量下降,错误信息如下:

The process cannot access the file because it is being used by another process.

我理解这是文件被其他进程锁定,但每个消息对应独立文件,仅共享目录,且目录创建代码已做重试处理。曾怀疑是否有其他进程同时获取消息(使用ReceiveAndDelete接收模式),但根据文档及测试,Microsoft SDK和ServiceBus应确保仅一个接收者获取消息;若多进程获取,应出现重复错误或最终保存数据,但异常对应的消息既未存入文件也未写入数据库。

目前疑问:

  • Azure File Share是否存在节流机制,导致挂载卷出现该错误?
  • 可查看哪些日志或追踪信息排查相关文件问题?

参考代码

public void Store(string relativeFilePath, string content)
{
    var startDateTimeUtc = DateTimeOffset.Now.ToString();
    var fullPath = Path.Combine(fileStorageProviderCredentials.Paths.Path, relativeFilePath);
    try
    {

        var directory = Path.GetDirectoryName(fullPath);
        if (!Directory.Exists(directory))
        {
            CreateDirectory(directory);
        }
        WriteFile(fullPath, content);
    }
    catch (Exception e)
    {
        var endDateTimeUtc = DateTimeOffset.Now.ToString();
        if (e.Message.Contains("directory"))
        {
            throw new FileStorageProviderSaveException("Start Time: " + startDateTimeUtc + ", End Time " + endDateTimeUtc + ", error when creating directory" + fullPath, e);
        }
        else
        {
            throw new FileStorageProviderSaveException("Start Time: " + startDateTimeUtc + ", End Time " + endDateTimeUtc + ", error when writing file" + fullPath, e);
        }
    }
}

private void CreateDirectory(string directory)
{
    var startDateTimeUtc = DateTimeOffset.Now.ToString();
    var dir_created = false;
    var retries = 0;
    while (dir_created == false && retries < 10)
    {
        try
        {
            var dir = Directory.CreateDirectory(directory);
            dir_created = true;
        }
        catch (Exception e)
        {
            Thread.Sleep(100 * retries); //Sleep for 100 milliseconds
            if (retries == 19)
            {
                var endDateTimeUtc = DateTimeOffset.Now.ToString();
                throw new FileStorageProviderSaveException("Start Time: " + startDateTimeUtc + ", End Time " + endDateTimeUtc + ", error creating directory file " + directory, e);
            }
        }
        retries++;
    }
}

private void WriteFile(string fullPath, string content)
{
    var startDateTimeUtc = DateTimeOffset.Now.ToString();
    var fileCreated = false;
    var retries = 0;
    while (!fileCreated && retries < 20)
    {
        try
        {
            File.WriteAllText(fullPath, content, Encoding.UTF8);
            fileCreated = true;
        }
        catch (Exception e)
        {
            Thread.Sleep(100 * retries); //Sleep for 100 milliseconds
            if (retries == 19)
            {
                var endDateTimeUtc = DateTimeOffset.Now.ToString();
                throw new FileStorageProviderSaveException("Start Time: " + startDateTimeUtc + ", End Time " + endDateTimeUtc + ", error writing file " + fullPath, e);
            }
        }
        retries++;
    }
}

测试期间图表

  • 文件共享性能图:
    文件共享使用情况图表
  • 文件创建时间图:
    文件创建时间图表

内容的提问来源于stack exchange,提问作者Jordan Dantas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 20:39:51