AequilibraE/aequilibrae

Closing AequilibraeMatrix doesnt free all resources

Closed this issue · 0 comments

AequilibraeMatrix has references to np.memmap objects which keeps file pointers open. When calling AequilibraeMatrix().close(), I'd expect these memmaps to also close. However, this doesn't happen. Consider the following:

matrix = AequilibraeMatrix()
matrix.create_empty(
    file_name="matrix.aem",
    zones=1,
    matrix_names=["mat"],
    index_names=["index"],
)
matrix.close()
AequilibraeMatrix().create_empty(
    file_name="matrix.aem",
    zones=1,
    matrix_names=["mat"],
    index_names=["index"],
)

On windows, the second create_empty call results in a OSError: [Errno 22] Invalid argument Exception due to the file already being opened in write mode.

the close method does not clean up all references to the memory map, it current cleans matrices and index, but not indices and matrix, which each also hold a reference to one of the np.memmaps

Furthermore, the matrix dictionary holds views to the underlying memmap. These views are subsequently picked up during traffic assignment and may have a lifespan beyond that of the AequilibraeMatrix, causing the aem file to remain open after the AequilibraeMatrix gets deleted / out of scope. My suggestion would be to create a copy of these views in __getattr__ so that the references to the np.memmap do not leave the AequilibraeMatrix class, and we can properly clean them up.