Browse Source

Fix race condition crash on concurrent accesses to the same file

https://github.com/dutchcoders/transfer.sh/issues/380
at-2021
JustAnotherArchivist 2 years ago
parent
commit
e4132f625b
2 changed files with 8 additions and 13 deletions
  1. +6
    -11
      server/handlers.go
  2. +2
    -2
      server/server.go

+ 6
- 11
server/handlers.go View File

@@ -625,23 +625,18 @@ func (metadata Metadata) remainingLimitHeaderValues() (remainingDownloads, remai
return remainingDownloads, remainingDays
}

func (s *Server) Lock(token, filename string) error {
func (s *Server) Lock(token, filename string) {
key := path.Join(token, filename)

if _, ok := s.locks[key]; !ok {
s.locks[key] = &sync.Mutex{}
}

s.locks[key].Lock()

return nil
lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
lock.(*sync.Mutex).Lock()
}

func (s *Server) Unlock(token, filename string) error {
func (s *Server) Unlock(token, filename string) {
key := path.Join(token, filename)
s.locks[key].Unlock()

return nil
lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
lock.(*sync.Mutex).Unlock()
}

func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (Metadata, error) {


+ 2
- 2
server/server.go View File

@@ -282,7 +282,7 @@ type Server struct {

profilerEnabled bool

locks map[string]*sync.Mutex
locks sync.Map

maxUploadSize int64
rateLimitRequests int
@@ -321,7 +321,7 @@ type Server struct {

func New(options ...OptionFn) (*Server, error) {
s := &Server{
locks: map[string]*sync.Mutex{},
locks: sync.Map{},
}

for _, optionFn := range options {


Loading…
Cancel
Save