Skip to content

Commit

Permalink
Add a lock on ZFS context manager
Browse files Browse the repository at this point in the history
Calls to libzfs are not thread-safe, make this as an attempt/helper to
serialize the calls.
  • Loading branch information
william-gr committed Mar 14, 2018
1 parent 439cc9e commit bc687e9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libzfs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ from libc.stdint cimport uintptr_t
from libc.string cimport memset, strncpy
from libc.stdlib cimport free, realloc

GLOBAL_CONTEXT_LOCK = threading.Lock()


include "nvpair.pxi"
include "converter.pxi"
Expand Down Expand Up @@ -357,6 +359,21 @@ cdef class ZFS(object):
with nogil:
libzfs.zprop_iter(self.__iterate_props, <void*>&iter, True, True, c_type)

def __enter__(self):
GLOBAL_CONTEXT_LOCK.acquire()
return self

def __exit__(self, exc_type, value, traceback):
self.__libzfs_fini()
GLOBAL_CONTEXT_LOCK.release()
if exc_type is not None:
raise

def __libzfs_fini(self):
if self.handle:
libzfs.libzfs_fini(self.handle)
self.handle = NULL

def __dealloc__(self):
libzfs.libzfs_fini(self.handle)

Expand Down

0 comments on commit bc687e9

Please sign in to comment.