From 96c9cd5c3925896b54264ccc4727a535e2ca7a6c Mon Sep 17 00:00:00 2001 From: Neil Horman Date: Wed, 24 May 2017 11:11:23 -0400 Subject: [PATCH] Ensure that a device mapper task is referenced until task is complete DeviceMapper tasks in go use SetFinalizer to clean up C construct counterparts in the C LVM library. While thats well and good, it relies heavily on the exact interpretation of when the golang garbage collector determines that an object is unreachable is subject to reclaimation. While common sense would assert that for stack variables (which these DM tasks always are), are unreachable when the stack frame in which they are declared returns, thats not the case. According to this: https://golang.org/pkg/runtime/#SetFinalizer The garbage collector decides that, if a function calls into a systemcall (which task.run() always will in LVM), and there are no subsequent references to the task variable within that stack frame, then it can be reclaimed. Those conditions are met in several devmapper.go routines, and if the garbage collector runs in the middle of a deviceMapper operation, then the task can be destroyed while the operation is in progress, leading to crashes, failed operations and other unpredictable behavior. The fix is to use the KeepAlive interface: https://golang.org/pkg/runtime/#KeepAlive The KeepAlive method is effectively an empy reference that fools the garbage collector into thinking that a variable is still reachable. By adding a call to KeepAlive in the task.run() method, we can ensure that the garbage collector won't reclaim a task object until its execution within the deviceMapper C library is complete. Signed-off-by: Neil Horman (cherry picked from commit d764d8b16624e4924b3949273089f851efa0f717) --- components/engine/pkg/devicemapper/devmapper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/components/engine/pkg/devicemapper/devmapper.go b/components/engine/pkg/devicemapper/devmapper.go index f0a289e098..ef7dba840d 100644 --- a/components/engine/pkg/devicemapper/devmapper.go +++ b/components/engine/pkg/devicemapper/devmapper.go @@ -155,6 +155,7 @@ func (t *Task) run() error { if res := DmTaskRun(t.unmanaged); res != 1 { return ErrTaskRun } + runtime.KeepAlive(t) return nil }