From 76900ae2914a75ad14ad902d660a3432441e962b Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Mon, 3 Nov 2014 21:02:55 +0000 Subject: [PATCH 1/2] Fix and relax locking --- internal/model/model.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/internal/model/model.go b/internal/model/model.go index 60e3614fa..bb08f3262 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -733,14 +733,13 @@ func (m *Model) ConnectedTo(deviceID protocol.DeviceID) bool { func (m *Model) GetIgnores(folder string) ([]string, error) { var lines []string + m.fmut.RLock() cfg, ok := m.folderCfgs[folder] + m.fmut.RUnlock() if !ok { return lines, fmt.Errorf("Folder %s does not exist", folder) } - m.fmut.Lock() - defer m.fmut.Unlock() - fd, err := os.Open(filepath.Join(cfg.Path, ".stignore")) if err != nil { if os.IsNotExist(err) { @@ -1239,10 +1238,9 @@ func (m *Model) Override(folder string) { // This is guaranteed to increment if the contents of the local folder has // changed. func (m *Model) CurrentLocalVersion(folder string) uint64 { - m.fmut.Lock() - defer m.fmut.Unlock() - + m.fmut.RLock() fs, ok := m.folderFiles[folder] + m.fmut.RUnlock() if !ok { // The folder might not exist, since this can be called with a user // specified folder name from the REST interface. @@ -1256,8 +1254,8 @@ func (m *Model) CurrentLocalVersion(folder string) uint64 { // sent by remote peers. This is guaranteed to increment if the contents of // the remote or global folder has changed. func (m *Model) RemoteLocalVersion(folder string) uint64 { - m.fmut.Lock() - defer m.fmut.Unlock() + m.fmut.RLock() + defer m.fmut.RUnlock() fs, ok := m.folderFiles[folder] if !ok { @@ -1275,10 +1273,9 @@ func (m *Model) RemoteLocalVersion(folder string) uint64 { } func (m *Model) availability(folder string, file string) []protocol.DeviceID { - m.fmut.Lock() - defer m.fmut.Unlock() - + m.fmut.RLock() fs, ok := m.folderFiles[folder] + m.fmut.RUnlock() if !ok { return nil } From fc4756298355c35b56b2104e293974b8a6935284 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Fri, 31 Oct 2014 23:41:18 +0000 Subject: [PATCH 2/2] Only connected devices are available devices --- internal/model/model.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/model/model.go b/internal/model/model.go index bb08f3262..2650e9ee4 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -1273,6 +1273,11 @@ func (m *Model) RemoteLocalVersion(folder string) uint64 { } func (m *Model) availability(folder string, file string) []protocol.DeviceID { + // Acquire this lock first, as the value returned from foldersFiles can + // gen heavily modified on Close() + m.pmut.RLock() + defer m.pmut.RUnlock() + m.fmut.RLock() fs, ok := m.folderFiles[folder] m.fmut.RUnlock() @@ -1280,7 +1285,14 @@ func (m *Model) availability(folder string, file string) []protocol.DeviceID { return nil } - return fs.Availability(file) + availableDevices := []protocol.DeviceID{} + for _, device := range fs.Availability(file) { + _, ok := m.protoConn[device] + if ok { + availableDevices = append(availableDevices, device) + } + } + return availableDevices } func (m *Model) String() string {