using Filestream - content of the file vanishes

P

Piotrekk

Hi

Probability of this phenomena is like 1/200.
The only code where I am dealing with file is the following:

using (FileStream fs = new FileStream(this.FilePath,
FileMode.Open,FileAccess.Read))
{
try
{
// This is web service call to check if i user has
permissions to upload file
myObj.BeforeUpload(this.FileName,
this.ServerPath);

while ((this.Offset < this.FileSize) && !
this.Stop)
{
fs.Seek(Offset, SeekOrigin.Begin);
buffer = new byte[32 * 1024];
BytesRead = fs.Read(buffer, 0, buffer.Length);

byte[] buff = new byte[BytesRead];

Copy(buffer, 0, buff, 0, BytesRead);

myObj.UploadChunk(this.FileName,
this.ServerPath, this.Offset, buff);

this.Offset += buff.Length;

//report progress to backgroundworker
bgw.ReportProgress((int)(((decimal)Offset /
(decimal)FileSize) * 100));
}
}
finally
{
fs.Close();
}
}


Question is. Why sometimes such an operation leaves my file empty.
This is upload file code. I have left every line unchanged. If you
have more skills than me try to explain this phenomena because I have
no more ideas.
Regards

ps:( I would be really grateful for an explanation )

Piotr Kolodziej
 
L

Laura T.

Your code cannot truncate the file, unless there is a nice big bug in the
FileStream, but I doubt it.
So the file truncation must come from somewhere else, maybe a sharing or
race condition.
If you change the to

using(FileStream fs=new
FileStream(this.FilePath,FileMode.Open,FileAccess.Read,FileShare.None))

In this way you deny any sharing of the file so no one can truncate it..
unless some fs filter kicks in (like virus scanners).

But this is just a huge guess.
 
P

Piotrekk

Your code cannot truncate the file, unless there is a nice big bug in the
FileStream, but I doubt it.
So the file truncation must come from somewhere else, maybe a sharing or
race condition.
If you change the to

using(FileStream fs=new
FileStream(this.FilePath,FileMode.Open,FileAccess.Read,FileShare.None))

In this way you deny any sharing of the file so no one can truncate it..
unless some fs filter kicks in (like virus scanners).

But this is just a huge guess.


Thanks for your post. I forgot to say that this weird truncation
happens when I am uploading the same file many times one by one ( to
test reaction of SQL - which is noticing the reception ). I did it for
example 20 times, in one minute, and when i attempt to do it again I
see ( in openFileDialog ) that file is truncated. What do you think
about 'sharing' or 'race condition' now? I know what race condition is
but I have never been thinking about this in file categories.
 
B

Ben Voigt [C++ MVP]

Piotrekk said:
Thanks for your post. I forgot to say that this weird truncation
happens when I am uploading the same file many times one by one ( to
test reaction of SQL - which is noticing the reception ). I did it for
example 20 times, in one minute, and when i attempt to do it again I
see ( in openFileDialog ) that file is truncated. What do you think
about 'sharing' or 'race condition' now? I know what race condition is
but I have never been thinking about this in file categories.


In any case it isn't the code you've shown, because you open the file
read-only. You're either opening the file somewhere else, or you have disk
corruption and possible hardware failure.
 
P

Piotrekk

In any case it isn't the code you've shown, because you open the file
read-only. You're either opening the file somewhere else, or you have disk
corruption and possible hardware failure.

Is that possible that the same code is trying to open this file while
the previous thread ( the same code ) hasn't finished with it yet?
This phenomena happens only when I upload the same file saveral times
in a row. Maybe OS is doing something with the opened and closed file
and another upload
form is trying to read it?

No disk corruption occured since this happens also on my new computer.
 
B

Ben Voigt [C++ MVP]

Piotrekk said:
Is that possible that the same code is trying to open this file while
the previous thread ( the same code ) hasn't finished with it yet?
This phenomena happens only when I upload the same file saveral times
in a row. Maybe OS is doing something with the opened and closed file
and another upload
form is trying to read it?

No disk corruption occured since this happens also on my new computer.

Are you catching any exceptions?
 
P

Piotrekk

Are you catching any exceptions?

Nope. No exceptions at all. Everything seems to work fine. Files are
uploaded. When I only experiment with one file ( upload ) too
much ...this happens.
 
P

Piotrekk

Are you catching any exceptions?

Nope. No exceptions at all. Everything seems to work fine. Files are
uploaded. When I only experiment with one file ( upload ) too
much ...this happens.

UploadForm code is available here for those of You who might want to
look at the complete code. It's not long thus it makes it hard to find
such a bug.
http://mion.elka.pw.edu.pl/~pkolodzi/UploadForm.cs

Regards
Piotr Kolodziej
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top