Repository: corteva/rioxarray

Issue:
open_rasterio fails when lock=True
https://corteva.github.io/rioxarray/stable/rioxarray.html#rioxarray-open-rasterio says using `open_rasterio` with `lock=True` is valid, but doing so gives an error. This option is also described in https://corteva.github.io/rioxarray/stable/examples/read-locks.html.

```
import rioxarray

url = (
    "https://naipeuwest.blob.core.windows.net/naip/v002/md/2013/md_100cm_2013/"
    "39076/m_3907617_ne_18_1_20130924.tif"
)
ds = rioxarray.open_rasterio(url, lock=True, chunks=(4, "auto", -1))
```
Output:
```
   File "<stdin>", line 1, in <module>
  File "/venv/lib/python3.8/site-packages/rioxarray/_io.py", line 751, in open_rasterio
    riods = manager.acquire()
  File "/venv/lib/python3.8/site-packages/xarray/backends/file_manager.py", line 181, in acquire
    file, _ = self._acquire_with_cache_info(needs_lock)
  File "/venv/lib/python3.8/site-packages/xarray/backends/file_manager.py", line 197, in _acquire_with_cache_info
    with self._optional_lock(needs_lock):
  File "/opt/conda/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/venv/lib/python3.8/site-packages/xarray/backends/file_manager.py", line 161, in _optional_lock
    with self._lock:
AttributeError: __enter__

```
Using
ubuntu 20.04
rioxarray==0.3.1
xarray==0.17.0


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/corteva/rioxarray/pull/274?src=pr&el=h1) Report
> Merging [#274](https://codecov.io/gh/corteva/rioxarray/pull/274?src=pr&el=desc) (b277d21) into [master](https://codecov.io/gh/corteva/rioxarray/commit/fa4a9358bd24493c08b54ee55ab0e64f9ab2f360?el=desc) (fa4a935) will **decrease** coverage by `0.73%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/corteva/rioxarray/pull/274/graphs/tree.svg?width=650&height=150&src=pr&token=C8QK7VO1OF)](https://codecov.io/gh/corteva/rioxarray/pull/274?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #274      +/-   ##
==========================================
- Coverage   93.94%   93.20%   -0.74%     
==========================================
  Files          12       12              
  Lines        1354     1354              
==========================================
- Hits         1272     1262      -10     
- Misses         82       92      +10     
```


| [Impacted Files](https://codecov.io/gh/corteva/rioxarray/pull/274?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [rioxarray/\_io.py](https://codecov.io/gh/corteva/rioxarray/pull/274/diff?src=pr&el=tree#diff-cmlveGFycmF5L19pby5weQ==) | `85.47% <100.00%> (-2.20%)` | :arrow_down: |
| [rioxarray/raster\_array.py](https://codecov.io/gh/corteva/rioxarray/pull/274/diff?src=pr&el=tree#diff-cmlveGFycmF5L3Jhc3Rlcl9hcnJheS5weQ==) | `97.56% <0.00%> (-0.70%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/corteva/rioxarray/pull/274?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/corteva/rioxarray/pull/274?src=pr&el=footer). Last update [fa4a935...b277d21](https://codecov.io/gh/corteva/rioxarray/pull/274?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/examples/read-locks.ipynb b/docs/examples/read-locks.ipynb
index 4a78b375..99015806 100644
--- a/docs/examples/read-locks.ipynb
+++ b/docs/examples/read-locks.ipynb
@@ -150,7 +150,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "With `lock=True` the initial read is slower (since some threads are waiting around for a lock)."
+    "By default and when a lock is passed in, the initial read is slower (since some threads are waiting around for a lock)."
    ]
   },
   {
@@ -201,7 +201,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "If you're reapeatedly reading subsets of the data, consider using `lock=True` or `lock=some_lock_object` to benefit from the caching."
+    "If you're reapeatedly reading subsets of the data, using the default lock or `lock=some_lock_object` to benefit from the caching."
    ]
   }
  ],
diff --git a/docs/history.rst b/docs/history.rst
index 7e22cc76..f7475a45 100644
--- a/docs/history.rst
+++ b/docs/history.rst
@@ -4,6 +4,10 @@ History
 Latest
 ------
 
+0.3.2
+-----
+- BUG: handle lock=True in open_rasterio (issue #273)
+
 0.3.1
 ------
 - BUG: Compatibility changes with xarray 0.17 (issue #254)
diff --git a/rioxarray/_io.py b/rioxarray/_io.py
index 3bac1a15..db7333b4 100644
--- a/rioxarray/_io.py
+++ b/rioxarray/_io.py
@@ -681,7 +681,7 @@ def open_rasterio(
         If chunks is provided, this argument is used to ensure that only one
         thread per process is reading from a rasterio file object at a time.
 
-        When ``lock=True`` or a lock instance is provided,
+        By default and when a lock instance is provided,
         a :class:`xarray.backends.CachingFileManager` is used to cache File objects.
         Since rasterio also caches some data, this will make repeated reads from the
         same object fast.
@@ -732,7 +732,7 @@ def open_rasterio(
             warp_extras=vrt.warp_extras,
         )
 
-    if lock is None:
+    if lock in (True, None):
         lock = RASTERIO_LOCK
     elif lock is False:
         lock = NO_LOCK


Test patch:
diff --git a/test/integration/test_integration__io.py b/test/integration/test_integration__io.py
index 922b1272..7dd6757f 100644
--- a/test/integration/test_integration__io.py
+++ b/test/integration/test_integration__io.py
@@ -971,3 +971,10 @@ def test_lockless():
         os.path.join(TEST_INPUT_DATA_DIR, "PLANET_SCOPE_3D.nc"), lock=False, chunk=True
     ) as rds:
         rds.mean().compute()
+
+
+def test_lock_true():
+    with rioxarray.open_rasterio(
+        os.path.join(TEST_INPUT_DATA_DIR, "PLANET_SCOPE_3D.nc"), lock=True, chunk=True
+    ) as rds:
+        rds.mean().compute()