moby/layer
Kir Kolyshkin af433dd200 layer: protect from same-name races
As pointed out by Tonis, there's a race between ReleaseRWLayer()
and GetRWLayer():

```
----- goroutine 1 -----               ----- goroutine 2 -----
ReleaseRWLayer()
  m := ls.mounts[l.Name()]
  ...
  m.deleteReference(l)
  m.hasReferences()
  ...                                 GetRWLayer()
  ...                                   mount := ls.mounts[id]
  ls.driver.Remove(m.mountID)
  ls.store.RemoveMount(m.name)          return mount.getReference()
  delete(ls.mounts, m.Name())
-----------------------               -----------------------
```

When something like this happens, GetRWLayer will return
an RWLayer without a storage. Oops.

There might be more races like this, and it seems the best
solution is to lock by layer id/name by using pkg/locker.

With this in place, name collision could not happen, so remove
the part of previous commit that protected against it in
CreateRWLayer (temporary nil assigmment and associated rollback).

So, now we have
* layerStore.mountL sync.Mutex to protect layerStore.mount map[]
  (against concurrent access);
* mountedLayer's embedded `sync.Mutex` to protect its references map[];
* layerStore.layerL (which I haven't touched);
* per-id locker, to avoid name conflicts and concurrent operations
  on the same rw layer.

The whole rig seems to look more readable now (mutexes use is
straightforward, no nested locks).

Reported-by: Tonis Tiigi <tonistiigi@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-05-21 10:54:28 -07:00
..
empty.go Add canonical import comment 2018-02-05 16:51:57 -05:00
empty_test.go Add canonical import comment 2018-02-05 16:51:57 -05:00
filestore.go layer: relax graphdriver ID format 2018-06-10 10:05:26 -07:00
filestore_test.go Remove metadata store interface 2018-03-06 10:42:00 -08:00
filestore_unix.go Add canonical import comment 2018-02-05 16:51:57 -05:00
filestore_windows.go Add canonical import comment 2018-02-05 16:51:57 -05:00
layer.go add ApplyDiff to RWLayer 2019-04-25 11:25:22 -04:00
layer_store.go layer: protect from same-name races 2019-05-21 10:54:28 -07:00
layer_store_windows.go Add canonical import comment 2018-02-05 16:51:57 -05:00
layer_test.go Remove metadata store interface 2018-03-06 10:42:00 -08:00
layer_unix.go Add canonical import comment 2018-02-05 16:51:57 -05:00
layer_unix_test.go Add canonical import comment 2018-02-05 16:51:57 -05:00
layer_windows.go Fix a typos in layer_windows.go 2018-11-18 18:45:06 +08:00
migration.go layer/CreateRWLayerByGraphID: remove 2019-05-21 10:54:28 -07:00
migration_test.go layer/CreateRWLayerByGraphID: remove 2019-05-21 10:54:28 -07:00
mount_test.go add ApplyDiff to RWLayer 2019-04-25 11:25:22 -04:00
mounted_layer.go layer: protect mountedLayer.references 2019-05-06 10:49:59 -07:00
ro_layer.go daemon: access to distribution internals 2018-06-10 10:05:26 -07:00
ro_layer_windows.go Add canonical import comment 2018-02-05 16:51:57 -05:00