StreamWriter 和 Samba2 (SMB2)
好吧,伙计们,这是一个艰难的挑战。
场景:
- 我在多台机器上运行多个服务
- 每个服务都有多个线程,每个线程在 FILER 上写入一个文件 - 我的机器使用的共享存储(使用诸如 \\filername 之类的共享) \foo\bar)
- FILER 机器是 NetApp 机器
- FILER 和运行服务的机器都使用 SMB2 (http://en.wikipedia.org/wiki/Server_Message_Block)
- 用于写入文件的指令非常简单那个下面列出了[代码]
[代码]
using (StreamWriter outfile = new StreamWriter(pathToTheFile, false))
{
outfile.Write(stringToWriteInTheFile);
}
[/代码]
问题:
有时服务仍“卡在”此指令上。给出的错误是:
进程无法访问文件'\\filername\foo\bar\myfile.txt',因为它正在被另一个进程使用。
发生其中一些错误后,服务拒绝释放文件上的锁定。然后会发生什么?
您可以删除该文件,但该文件会立即重新创建。就像某种永久流处于活动状态并无限期地继续写入文件一样。
你可以停止服务:它被卡住了,不会被停止,所以我在 2 分钟后强制执行 Thread.Abort (是的,我知道,但是练习,但还能做什么?)。
因此,服务现在已停止,但计算机保留了文件的句柄,除非重新启动计算机,否则您无法终止保持句柄处于活动状态的进程。 。 。
我现在不知道该怎么办,我想我已经尝试了一切。
注意事项:
以前,FILER 和机器都使用 SMB1,并且从未出现此问题。所以我猜想后台发生了一些可疑的事情,但我无法理解是什么......
我最近更改了用于编写文件的代码,拼命尝试将所有内容“委托”给.net。现在是这样:
File.WriteAllText(pathToTheFile, stringToWriteInTheFile);
但我的直觉是,在幕后,.net 正在做完全相同的事情 - 不过变化是最近发生的,所以我仍然不能说“修复”是否有效。
编辑(根据 Vash 评论):通常文件是不同的,但有时可能会发生(而且确实发生)多个线程试图写入同一个文件,但是:( - 执行 File.WriteAllText 不应该小心并发问题?
Ok guys, this one is a tough one.
The scenario:
- I have multiple services running on multiple machines
- Each service has multiple threads, and each thread writes a file on a FILER - the shared storage used by my machines (using a share such as \\filername\foo\bar)
- The FILER machine is a NetApp machine
- Both the FILER and the machines running the services are using SMB2 (http://en.wikipedia.org/wiki/Server_Message_Block)
- The instruction used to write the file is as simple as the one listed below in [THE CODE]
[THE CODE]
using (StreamWriter outfile = new StreamWriter(pathToTheFile, false))
{
outfile.Write(stringToWriteInTheFile);
}
[/THE CODE]
The problem:
Sometimes the service remains "stuck" on this instruction. The error given is:
The process cannot access the file '\\filername\foo\bar\myfile.txt' because it is being used by another process.
After some of these errors, the service refuses to release the lock on the file. What happens then?
You can delete the file, but the file is IMMEDIATELY recreated. Like if a sort-of permanent Stream is alive and keeps writing the file indefinitely.
You can stop the service: it's stuck, and won't be stopped, so I forced a Thread.Abort (yeah, I know but practice, but what else?) after 2 minutes.
So, the service is now stopped, but the machine retains an handle to the file and you CANNOT kill the process keeping the handle alive except by rebooting the machine. . .
I don't know what to do right now, I think I tried everything.
Considerations:
Previously, the FILER and the machines were using SMB1, and this problem never arised. So I guess something fishy happens in the background, but I can't understand what...
I changed recently the code used to write the file, in a desperate attempt to "delegate" everything to .net. Now it's:
File.WriteAllText(pathToTheFile, stringToWriteInTheFile);
but my gut feeling is that, under the wraps, .net is doing the exact same thing - the change is quite recent though, so I can't still say if the "fix" is working or not.
EDIT (as per Vash comment): Usually the file is different, but it can happen (and it actually happens) sometimes that multiple threads are trying to write the same file, however :( - doing the File.WriteAllText shouldn't take care of concurrency issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以“独占”模式显式打开 FileStream,即,
当然,您的代码必须预见到文件在写入时可能会被锁定并做出适当的反应。该部分留给读者作为练习:-)
免责声明:我已经在多线程环境中使用了它,但我不能保证它可以在 Samba 上工作。
Try explicitly opening a FileStream in "exclusive" mode, ie
Of course your code will have to anticipate that the file might be locked when it goes to write it and react appropriately. That part is left as an exercise for the reader :-)
Disclaimer: I have used this in a multi-threaded environment, but I can't guarantee it will work over Samba.