We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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()
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `tiledb/array.py`
Content:
```
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 FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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

{"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"}], "after_files": [{"content": "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\nclass 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\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            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\n        obj = super(SparseArray, cls).__new__(cls, *args, **kwargs)\n        return obj\n", "path": "tiledb/array.py"}]}