Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: temp files left behind by test #3050

Closed
alexbrainman opened this issue Feb 17, 2012 · 12 comments
Closed

net/http: temp files left behind by test #3050

alexbrainman opened this issue Feb 17, 2012 · 12 comments
Milestone

Comments

@alexbrainman
Copy link
Member

Checking my /tmp directory, found these. It seems that os.RemoveAll runs when server it
still in flight sometimes.

Should I continue to report problems like that?

Alex
@rsc
Copy link
Contributor

rsc commented Feb 17, 2012

Comment 1:

Yes, please keep reporting these.

Labels changed: added priority-go1, removed priority-triage.

Owner changed to builder@golang.org.

@bradfitz
Copy link
Contributor

Comment 2:

I can't reproduce.
More details?

@alexbrainman
Copy link
Member Author

Comment 3:

I am seeing this on windows.
Running "go test" in pkg/net/http leaves file named like C:\Documents and
Settings\brainman\Local Settings\Temp\067641025\foo.txt with "Hello world" inside. I
suspect it happens during TestFileServerImplicitLeadingSlash. If you check error
returned by os.RemoveAll(tempDir), you will see that sometimes it fails. File delete
will fail on Windows, if it is opened. You have no code that sync using foo.txt and
deleting it - so sometimes these two clash.
I know it is pain in the back, but that how it works. People who will run this test
might endup with new file like 067641025\foo.txt in their temp directory every time the
do that.
Alex

@bradfitz
Copy link
Contributor

Comment 4:

That test starts like:
func TestFileServerImplicitLeadingSlash(t *testing.T) {
        tempDir, err := ioutil.TempDir("", "")
        if err != nil {
                t.Fatalf("TempDir: %v", err)
        }
        defer os.RemoveAll(tempDir)
        if err := ioutil.WriteFile(filepath.Join(tempDir, "foo.txt"), []byte("Hello world"), 0644); err != nil {
                t.Fatalf("WriteFile: %v", err)
        }
        ts := httptest.NewServer(StripPrefix("/bar/", FileServer(Dir(tempDir))))
        defer ts.Close()
The defer ts.Close() will run before the os.RemoveAll defer.  And all the HTTP requests
will be done by then.
So I'm confused.

@alexbrainman
Copy link
Member Author

Comment 5:

I didn't write that code, so it is hard for me to tell. But, as this issue demonstrates,
ts.Close completion does not guarantee that files are closed. If you think it is wrong,
then, I think, you have assumption somewhere in your code that does not stand.
You could try and see it for yourself. Alternatively, if you want me to try things, I
will do it. But you have to tell me what to check. I had quick look myself, but I am not
familiar with this code, so I got lost pretty quickly.
Alex

@bradfitz
Copy link
Contributor

Comment 6:

I'm travelling without a Windows machine for a day.  But here's a CL which might be
helpful or interesting:
http://golang.org/cl/5707056

@alexbrainman
Copy link
Member Author

Comment 7:

I ran this:
for i in $(seq 1 30); do echo "go test >>/c/tmp/a.txt 2>&1" ; done| bash
the output is attached. Your CL (http://golang.org/cl/5707056) - your panic is
in the log. There are other problems, I see in the log. I didn't see them before.
Perhaps, they are there because I run test 30 times. I could look at these, if you
interested. :-)
Alex
$ hg id
b4580a3b3331+ tip

Attachments:

  1. a.txt (229611 bytes)

@bradfitz
Copy link
Contributor

Comment 8:

Are you running a virus scanner?
I'd put my money on a race between the test creating the file, virus scanner noticing
new file to scan, and the test deleting it.

@alexbrainman
Copy link
Member Author

Comment 9:

Nope. I do not run virus scanner. Many other tests create files in temp directory, but
they all get cleaned up. Not this one.
Alex

@rsc
Copy link
Contributor

rsc commented Feb 29, 2012

Comment 10:

I suspect this is a race between the server closing the file
(after serving the response) and the test trying to remove it
(after reading the response).
If the remove happens before the close, it will fail on Windows
but not on Unix.
The solution then would be to wait for the server to shut down,
or to insert a short sleep.

@bradfitz
Copy link
Contributor

Comment 11:

Maybe http://golang.org/cl/5708066

@alexbrainman
Copy link
Member Author

Comment 12:

That does it. Thank you Brad.
Fixed by http://code.google.com/p/go/source/detail?r=8bdd36e45822.

Status changed to Fixed.

@rsc rsc added this to the Go1 milestone Apr 10, 2015
@rsc rsc removed the priority-go1 label Apr 10, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants