prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  likely race condition in DenseArray.__new__
  
  Test program below exposes (what I believe is a) race in class handling in DenseArray.  The test program is completely artificial - it just spins up 5 threads, each of which open an S3 object and ask its shape.   Without a sleep to sequence their execution, it fails inside the DenseArray.__new__ method.
  
  Run with the sleep statement in worker(), I see:
  ```
  python cctest.py
  starting... 0
  starting... 1
  working... 0
  ending... 0 (1305994, 972)
  starting... 2
  working... 1
  ending... 1 (1305994, 972)
  starting... 3
  working... 2
  ending... 2 (1305994, 972)
  starting... 4
  working... 3
  ending... 3 (1305994, 972)
  working... 4
  ending... 4 (1305994, 972)
  all done [(1305994, 972), (1305994, 972), (1305994, 972), (1305994, 972), (1305994, 972)]
  ```
  
  Run with the sleep in worker removed/commented out:
  ```
  $ python cctest.py
  starting... 0
  starting... 1
  starting... 2
  working... 0
  ending... 0 (1305994, 972)
  starting... 3
  working... 2
  ending... 2 (1305994, 972)
  starting... 4
  working... 3
  ending... 3 (1305994, 972)
  working... 4
  ending... 4 (1305994, 972)
  Traceback (most recent call last):
    File "cctest.py", line 33, in <module>
      main()
    File "cctest.py", line 27, in main
      res = [f.result() for f in concurrent.futures.as_completed(futures)]
    File "cctest.py", line 27, in <listcomp>
      res = [f.result() for f in concurrent.futures.as_completed(futures)]
    File "/usr/lib64/python3.7/concurrent/futures/_base.py", line 428, in result
      return self.__get_result()
    File "/usr/lib64/python3.7/concurrent/futures/_base.py", line 384, in __get_result
      raise self._exception
    File "/usr/lib64/python3.7/concurrent/futures/thread.py", line 57, in run
      result = self.fn(*self.args, **self.kwargs)
    File "cctest.py", line 11, in worker
      with tiledb.DenseArray(uri) as A:
    File "/home/ec2-user/venv/lib64/python3.7/site-packages/tiledb/array.py", line 10, in __new__
      DenseArray.__bases__ = DenseArray.__bases__ + (cloudarray.CloudArray,)
  TypeError: duplicate base class CloudArray
  ```
  
  Test case:
  ```
  import tiledb
  import time
  import numpy as np
  import concurrent.futures
  
  
  def worker(n, uri):
      print('starting...', n)
      # time.sleep(n)
  
      with tiledb.DenseArray(uri) as A:
          print('working...', n)
          res = A.shape
  
      print('ending...', n, res)
      return res
  
  
  def main():
      executor_cls = concurrent.futures.ThreadPoolExecutor
  
      uri = "s3://tiledb-eval/10x_mouse_13MM_processed-Xr3zstd.cxg/X"
  
      with executor_cls(max_workers=2) as executor:
          futures = [executor.submit(worker, n, uri) for n in range(0, 5)]
          res = [f.result() for f in concurrent.futures.as_completed(futures)]
  
      print('all done', res)
  
  
  if __name__ == '__main__':
      main()
  ```
  
  </issue>
  <code>
  [start of tiledb/array.py]
  1 from .libtiledb import DenseArrayImpl, SparseArrayImpl
  2 
  3 class DenseArray(DenseArrayImpl):
  4     _mixin_init = False
  5 
  6     def __new__(cls, *args, **kwargs):
  7         if not cls._mixin_init:
  8             try:
  9                 from tiledb.cloud import cloudarray
  10                 DenseArray.__bases__ = DenseArray.__bases__ + (cloudarray.CloudArray,)
  11             except ImportError:
  12                 pass
  13             finally:
  14                 cls._mixin_init = True
  15 
  16         obj = super(DenseArray, cls).__new__(cls, *args, **kwargs)
  17         return obj
  18 
  19 class SparseArray(SparseArrayImpl):
  20     _mixin_init = False
  21 
  22     def __new__(cls, *args, **kwargs):
  23         if not cls._mixin_init:
  24             try:
  25                 from tiledb.cloud import cloudarray
  26                 SparseArray.__bases__ = SparseArray.__bases__ + (cloudarray.CloudArray,)
  27             except ImportError:
  28                 pass
  29             finally:
  30                 cls._mixin_init = True
  31 
  32         obj = super(SparseArray, cls).__new__(cls, *args, **kwargs)
  33         return obj
  34
  [end of tiledb/array.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/tiledb/array.py b/tiledb/array.py
  --- a/tiledb/array.py
  +++ b/tiledb/array.py
  @@ -1,17 +1,29 @@
   from .libtiledb import DenseArrayImpl, SparseArrayImpl
   
  +# Extensible (pure Python) array class definitions inheriting from the
  +# Cython implemention. The cloudarray mix-in adds optional functionality
  +# for registering arrays and executing functions on the
  +
  +# NOTE: the mixin import must be inside the __new__ initializer because it
  +#       needs to be deferred. tiledb.cloud is not yet known to the importer
  +#       when this code is imported.
  +#       TODO: might be possible to work-around/simplify by using
  +#       import meta-hooks instead.
  +
   class DenseArray(DenseArrayImpl):
       _mixin_init = False
   
       def __new__(cls, *args, **kwargs):
           if not cls._mixin_init:
  +            # must set before importing, because import is not thread-safe
  +            #   https://github.com/TileDB-Inc/TileDB-Py/issues/244
  +            cls._mixin_init = True
               try:
                   from tiledb.cloud import cloudarray
                   DenseArray.__bases__ = DenseArray.__bases__ + (cloudarray.CloudArray,)
  +                DenseArray.__doc__ = DenseArrayImpl.__doc__
               except ImportError:
                   pass
  -            finally:
  -                cls._mixin_init = True
   
           obj = super(DenseArray, cls).__new__(cls, *args, **kwargs)
           return obj
  @@ -21,13 +33,13 @@
   
       def __new__(cls, *args, **kwargs):
           if not cls._mixin_init:
  +            cls._mixin_init = True
               try:
                   from tiledb.cloud import cloudarray
                   SparseArray.__bases__ = SparseArray.__bases__ + (cloudarray.CloudArray,)
  +                SparseArray.__doc__ = DenseArrayImpl.__doc__
               except ImportError:
                   pass
  -            finally:
  -                cls._mixin_init = True
   
           obj = super(SparseArray, cls).__new__(cls, *args, **kwargs)
           return obj
  
verification_info: {"golden_diff": "diff --git a/tiledb/array.py b/tiledb/array.py\n--- a/tiledb/array.py\n+++ b/tiledb/array.py\n@@ -1,17 +1,29 @@\n from .libtiledb import DenseArrayImpl, SparseArrayImpl\n \n+# Extensible (pure Python) array class definitions inheriting from the\n+# Cython implemention. The cloudarray mix-in adds optional functionality\n+# for registering arrays and executing functions on the\n+\n+# NOTE: the mixin import must be inside the __new__ initializer because it\n+#       needs to be deferred. tiledb.cloud is not yet known to the importer\n+#       when this code is imported.\n+#       TODO: might be possible to work-around/simplify by using\n+#       import meta-hooks instead.\n+\n class DenseArray(DenseArrayImpl):\n     _mixin_init = False\n \n     def __new__(cls, *args, **kwargs):\n         if not cls._mixin_init:\n+            # must set before importing, because import is not thread-safe\n+            #   https://github.com/TileDB-Inc/TileDB-Py/issues/244\n+            cls._mixin_init = True\n             try:\n                 from tiledb.cloud import cloudarray\n                 DenseArray.__bases__ = DenseArray.__bases__ + (cloudarray.CloudArray,)\n+                DenseArray.__doc__ = DenseArrayImpl.__doc__\n             except ImportError:\n                 pass\n-            finally:\n-                cls._mixin_init = True\n \n         obj = super(DenseArray, cls).__new__(cls, *args, **kwargs)\n         return obj\n@@ -21,13 +33,13 @@\n \n     def __new__(cls, *args, **kwargs):\n         if not cls._mixin_init:\n+            cls._mixin_init = True\n             try:\n                 from tiledb.cloud import cloudarray\n                 SparseArray.__bases__ = SparseArray.__bases__ + (cloudarray.CloudArray,)\n+                SparseArray.__doc__ = DenseArrayImpl.__doc__\n             except ImportError:\n                 pass\n-            finally:\n-                cls._mixin_init = True\n \n         obj = super(SparseArray, cls).__new__(cls, *args, **kwargs)\n         return obj\n", "issue": "likely race condition in DenseArray.__new__\n\r\nTest program below exposes (what I believe is a) race in class handling in DenseArray.  The test program is completely artificial - it just spins up 5 threads, each of which open an S3 object and ask its shape.   Without a sleep to sequence their execution, it fails inside the DenseArray.__new__ method.\r\n\r\nRun with the sleep statement in worker(), I see:\r\n```\r\npython cctest.py\r\nstarting... 0\r\nstarting... 1\r\nworking... 0\r\nending... 0 (1305994, 972)\r\nstarting... 2\r\nworking... 1\r\nending... 1 (1305994, 972)\r\nstarting... 3\r\nworking... 2\r\nending... 2 (1305994, 972)\r\nstarting... 4\r\nworking... 3\r\nending... 3 (1305994, 972)\r\nworking... 4\r\nending... 4 (1305994, 972)\r\nall done [(1305994, 972), (1305994, 972), (1305994, 972), (1305994, 972), (1305994, 972)]\r\n```\r\n\r\nRun with the sleep in worker removed/commented out:\r\n```\r\n$ python cctest.py\r\nstarting... 0\r\nstarting... 1\r\nstarting... 2\r\nworking... 0\r\nending... 0 (1305994, 972)\r\nstarting... 3\r\nworking... 2\r\nending... 2 (1305994, 972)\r\nstarting... 4\r\nworking... 3\r\nending... 3 (1305994, 972)\r\nworking... 4\r\nending... 4 (1305994, 972)\r\nTraceback (most recent call last):\r\n  File \"cctest.py\", line 33, in <module>\r\n    main()\r\n  File \"cctest.py\", line 27, in main\r\n    res = [f.result() for f in concurrent.futures.as_completed(futures)]\r\n  File \"cctest.py\", line 27, in <listcomp>\r\n    res = [f.result() for f in concurrent.futures.as_completed(futures)]\r\n  File \"/usr/lib64/python3.7/concurrent/futures/_base.py\", line 428, in result\r\n    return self.__get_result()\r\n  File \"/usr/lib64/python3.7/concurrent/futures/_base.py\", line 384, in __get_result\r\n    raise self._exception\r\n  File \"/usr/lib64/python3.7/concurrent/futures/thread.py\", line 57, in run\r\n    result = self.fn(*self.args, **self.kwargs)\r\n  File \"cctest.py\", line 11, in worker\r\n    with tiledb.DenseArray(uri) as A:\r\n  File \"/home/ec2-user/venv/lib64/python3.7/site-packages/tiledb/array.py\", line 10, in __new__\r\n    DenseArray.__bases__ = DenseArray.__bases__ + (cloudarray.CloudArray,)\r\nTypeError: duplicate base class CloudArray\r\n```\r\n\r\nTest case:\r\n```\r\nimport tiledb\r\nimport time\r\nimport numpy as np\r\nimport concurrent.futures\r\n\r\n\r\ndef worker(n, uri):\r\n    print('starting...', n)\r\n    # time.sleep(n)\r\n\r\n    with tiledb.DenseArray(uri) as A:\r\n        print('working...', n)\r\n        res = A.shape\r\n\r\n    print('ending...', n, res)\r\n    return res\r\n\r\n\r\ndef main():\r\n    executor_cls = concurrent.futures.ThreadPoolExecutor\r\n\r\n    uri = \"s3://tiledb-eval/10x_mouse_13MM_processed-Xr3zstd.cxg/X\"\r\n\r\n    with executor_cls(max_workers=2) as executor:\r\n        futures = [executor.submit(worker, n, uri) for n in range(0, 5)]\r\n        res = [f.result() for f in concurrent.futures.as_completed(futures)]\r\n\r\n    print('all done', res)\r\n\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n```\n", "before_files": [{"content": "from .libtiledb import DenseArrayImpl, SparseArrayImpl\n\nclass DenseArray(DenseArrayImpl):\n    _mixin_init = False\n\n    def __new__(cls, *args, **kwargs):\n        if not cls._mixin_init:\n            try:\n                from tiledb.cloud import cloudarray\n                DenseArray.__bases__ = DenseArray.__bases__ + (cloudarray.CloudArray,)\n            except ImportError:\n                pass\n            finally:\n                cls._mixin_init = True\n\n        obj = super(DenseArray, cls).__new__(cls, *args, **kwargs)\n        return obj\n\nclass SparseArray(SparseArrayImpl):\n    _mixin_init = False\n\n    def __new__(cls, *args, **kwargs):\n        if not cls._mixin_init:\n            try:\n                from tiledb.cloud import cloudarray\n                SparseArray.__bases__ = SparseArray.__bases__ + (cloudarray.CloudArray,)\n            except ImportError:\n                pass\n            finally:\n                cls._mixin_init = True\n\n        obj = super(SparseArray, cls).__new__(cls, *args, **kwargs)\n        return obj\n", "path": "tiledb/array.py"}]}