Esri / Esri/arcpy

Bug - Temporary raster from NumPyArrayToRaster is not cleared if Describe is used and raster is still in scope at end of script

Open
#9 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
57
Forks
5
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
If you use arcpy.NumPyArrayToRaster and save its result in a variable, then use arcpy.Describe, and then keep the raster variable in scope until the end of the script, it will not be cleared, and will remain on disk.

The same applies if the script does not end normally, but is ended by a KeyboardInterrupt.

**To Reproduce**
Run this code in the arcgis-propy3 environment.

```python
import os
import signal
from pathlib import Path

import arcpy
import numpy as np

TEST_ARRAY = np.ones((3, 3))
LOWER_LEFT = arcpy.Point(150_000, 400_000)
RD_NEW = arcpy.SpatialReference(28992)
NODATA_VALUE = -9999.0

def show_memory_leak_script_end():
with arcpy.EnvManager(outputCoordinateSystem=RD_NEW):
raster = arcpy.NumPyArrayToRaster(
in_array=TEST_ARRAY,
lower_left_corner=LOWER_LEFT,
x_cell_size=10,
y_cell_size=10,
value_to_nodata=NODATA_VALUE
)

print("MEMORY LEAK AT END OF SCRIPT")
print("-------------------------------")
print(f"Catalog Path: {arcpy.Describe(raster).catalogPath}")
print()
return raster, str(Path(arcpy.Describe(raster).catalogPath).parent)

def show_memory_leak_keyboard_interrupt():
with arcpy.EnvManager(outputCoordinateSystem=RD_NEW):
raster = arcpy.NumPyArrayToRaster(
in_array=TEST_ARRAY,
lower_left_corner=LOWER_LEFT,
x_cell_size=10,
y_cell_size=10,
value_to_nodata=NODATA_VALUE
)

print("MEMORY LEAK AT KEYBOARDINTERRUPT")
print("-------------------------------")
print(f"Catalog Path: {arcpy.Describe(raster).catalogPath}")
print()
os.kill(os.getpid(), signal.SIGINT)
# This file will still exist after the script ends.

if __name__ == "__main__":
# If raster_ is still in scope at the end of the script,
# the file will keep existing after the end of the script.
raster_, temp_workspace = show_memory_leak_script_end()
print(temp_workspace)

print(f"ALL RASTERS IN TEMPORARY STORAGE {temp_workspace}")
print("-------------------------------")
with arcpy.EnvManager(workspace=temp_workspace):
rasters = arcpy.ListRasters()
print(f"Raster Count: {len(rasters)}")
print(rasters)
print()

# If this is uncommented, the raster count increases by two per execution of the script.
# show_memory_leak_keyboard_interrupt()
```

**Expected behavior**
Since these rasters are normally temporary unless explicitly saved, I expect there to be no data representing the raster on my disk after finishing the script.

**Version (please complete the following information):**
- Pro 3.5.5

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.