moby--moby/layer
Kir Kolyshkin 936432326a
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>
(cherry picked from commit af433dd200)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-05-25 22:33:45 +02:00
..
empty.go
empty_test.go
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
filestore_windows.go
layer.go Remove metadata store interface 2018-03-06 10:42:00 -08:00
layer_store.go layer: protect from same-name races 2019-05-25 22:33:45 +02:00
layer_store_windows.go
layer_test.go Remove metadata store interface 2018-03-06 10:42:00 -08:00
layer_unix.go
layer_unix_test.go
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-25 22:33:37 +02:00
migration_test.go layer/CreateRWLayerByGraphID: remove 2019-05-25 22:33:37 +02:00
mount_test.go
mounted_layer.go layer: protect mountedLayer.references 2019-05-25 22:32:43 +02:00
ro_layer.go daemon: access to distribution internals 2018-06-10 10:05:26 -07:00
ro_layer_windows.go