c# - StreamWriter and Samba2 (SMB2) -


ok guys, 1 tough one.

the scenario:

  • i have multiple services running on multiple machines
  • each service has multiple threads, , each thread writes file on filer - shared storage used machines (using share such \\filername\foo\bar)
  • the filer machine netapp machine
  • both filer , machines running services using smb2 (http://en.wikipedia.org/wiki/server_message_block)
  • the instruction used write file simple 1 listed below in [the code]

[the code]

using (streamwriter outfile = new streamwriter(pathtothefile, false)) {   outfile.write(stringtowriteinthefile); } 

[/the code]

the problem:

sometimes service remains "stuck" on instruction. error given is:

the process cannot access file '\\filername\foo\bar\myfile.txt' because being used process.

after of these errors, service refuses release lock on file. happens then?

you can delete file, file recreated. if sort-of permanent stream alive , keeps writing file indefinitely.

you can stop service: it's stuck, , won't stopped, forced thread.abort (yeah, know practice, else?) after 2 minutes.

so, service stopped, machine retains handle file , cannot kill process keeping handle alive except rebooting machine. . .

i don't know right now, think tried everything.

considerations:

previously, filer , machines using smb1, , problem never arised. guess fishy happens in background, can't understand what...

i changed code used write file, in desperate attempt "delegate" .net. it's:

file.writealltext(pathtothefile, stringtowriteinthefile); 

but gut feeling that, under wraps, .net doing exact same thing - change quite recent though, can't still if "fix" working or not.

edit (as per vash comment): file different, can happen (and happens) multiple threads trying write same file, :( - doing file.writealltext shouldn't take care of concurrency issues?

try explicitly opening filestream in "exclusive" mode, ie

using (var fs = new filestream("path",                                filemode.open, fileaccess.readwrite,                                fileshare.none)) {     using (var sw = new streamwriter(fs))     {         ... 

of course code have anticipate file might locked when goes write , react appropriately. part left exercise reader :-)

disclaimer: have used in multi-threaded environment, can't guarantee work on samba.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -