Comments (8)
@jkowalski Can you please provide code to reproduce this behavior? And what WebDav server do you use?
from gowebdav.
package main
import (
"log"
"net/http"
"net/http/httptest"
"sync"
"github.com/studio-b12/gowebdav"
"golang.org/x/net/webdav"
)
func basicAuth(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if user, passwd, ok := r.BasicAuth(); ok {
if user == "user" && passwd == "password" {
h.ServeHTTP(w, r)
return
}
http.Error(w, "not authorized", 403)
} else {
w.Header().Set("WWW-Authenticate", `Basic realm="x"`)
w.WriteHeader(401)
}
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", basicAuth(&webdav.Handler{
FileSystem: webdav.Dir("/"),
LockSystem: webdav.NewMemLS(),
}))
server := httptest.NewServer(mux)
defer server.Close()
cli := gowebdav.NewClient(server.URL, "user", "password")
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
f, err := cli.ReadDir("/")
log.Printf("f: %v err: %v", f, err)
}()
}
wg.Wait()
}
Run this with go -race repro.go
and it prints:
╰─➤ go run -race repro 1 ↵
==================
WARNING: DATA RACE
Read at 0x00c00011cf50 by goroutine 9:
github.com/studio-b12/gowebdav.(*Client).req()
/Users/jarek/Projects/gowebdav/requests.go:45 +0x1276
github.com/studio-b12/gowebdav.(*Client).propfind()
/Users/jarek/Projects/gowebdav/requests.go:94 +0x15a
github.com/studio-b12/gowebdav.(*Client).ReadDir()
/Users/jarek/Projects/gowebdav/client.go:164 +0x1fb
main.main.func1()
/Users/jarek/Projects/repro/main.go:48 +0x9f
Previous write at 0x00c00011cf50 by goroutine 8:
github.com/studio-b12/gowebdav.(*Client).req()
/Users/jarek/Projects/gowebdav/requests.go:54 +0xd48
github.com/studio-b12/gowebdav.(*Client).propfind()
/Users/jarek/Projects/gowebdav/requests.go:94 +0x15a
github.com/studio-b12/gowebdav.(*Client).ReadDir()
/Users/jarek/Projects/gowebdav/client.go:164 +0x1fb
main.main.func1()
/Users/jarek/Projects/repro/main.go:48 +0x9f
Goroutine 9 (running) created at:
main.main()
/Users/jarek/Projects/repro/main.go:46 +0x4de
Goroutine 8 (running) created at:
main.main()
/Users/jarek/Projects/repro/main.go:46 +0x4de
The bug is because two different instances of req()
are both racing to update request headers stored in c.headers
I think there are 2 required fixes:
- Ensure you're setting
c.auth
only once - use mutex or sync.Once for that - Change
Authorize()
method to takehttp.Headers
and set authentication directly on each request instead of onClient
- move the call after copying headers fromc.headers
I actually have made a fix and will send PR shortly.
from gowebdav.
@jkowalski thank you for your report and patch.
i skimmed though your improvement.
it's definitely a solution.
Here are my points which I have to think more about.
- API changes in Authorize. I'd love to avoid it.
- Losing the context (Client) in Authorize.
- The Mutext, which is just needed for concurrency.
from gowebdav.
Any updates on this? without a fix in #37 I can reproduce this issue very frequently.
from gowebdav.
We are getting the same error and I would love to see the fix merged :)
from gowebdav.
@jozuenoon I suppose you could use @jkowalski's fork for the time being.
@jkowalski What is the reason for moving to http.Request from Client?
Also, in requests.go, why are the Authorize call and the Header.Add loop rearranged? It's been quite a while since I've worked on this but if I remember correctly, that order was important.
from gowebdav.
The race condition is due to unsafe changes to c.auth
and parallel modifications to the Client from two separate goroutines.
While i'm sure this could be made safer with a mutex around client, it's not really necessary to share the client (for writes) at all, since all requests are individually authorized anyway. The client in this case just provides additional headers to be set on each request, which is what my PR is doing.
from gowebdav.
@jkowalski thank you!
I changed my mind and applied your patch to keep the library working.
we should face #38 as next task.
Welcome!
from gowebdav.
Related Issues (20)
- Wrong content of uploaded file
- Www-Authenticate header's token should be treated as case-insensitive HOT 3
- File upload uploads files with 0 bytes - No error given HOT 9
- improve concurrency
- ReadDir omits returning the first result if it is a directory HOT 1
- Error on Folders with '+' HOT 3
- 400 when passing whitespace in path args to c.Copy()
- Set GetBody on http.Request HOT 2
- support Walk func HOT 1
- [err:WriteStream] file write problem: err:WriteStream HOT 2
- No support for Bearer Token authentication HOT 2
- [WIP] Supports upload files via WriteCloser
- Support for cookies HOT 3
- Authentication fails when deployed to Windows machine without Go installed HOT 4
- This is a library usage question,about ContentType. HOT 1
- [BUG/FEATURE] OneDrive Support HOT 2
- Digest auth usage HOT 2
- Use the tag Management release version to publish HOT 2
- Stat() for directory does not return last modified time HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gowebdav.