Skip to content

memory leak for uncompressed .trx files. #108

@skoudoro

Description

@skoudoro

TrxFile.close() seems to silently skips mmap cleanup for uncompressed .trx files.

I catched that when working on dipy/dipy#3808

here is a basic example

  import tempfile                                                                                                                                              
  import os                                                                                                                                                    
  import numpy as np                                                                                                                                           
  import trx.trx_file_memmap as tmm                                                                                                                            
                  
  trx = tmm.TrxFile(nb_streamlines=2, nb_vertices=6)
  trx.streamlines._data[:] = np.random.rand(6, 3).astype(np.float32)
  trx.streamlines._offsets[:] = np.array([0, 3], dtype=np.uint32)
  trx.streamlines._lengths[:] = np.array([3, 3], dtype=np.uint32)

  with tempfile.TemporaryDirectory() as tmp:
      trx_path = os.path.join(tmp, "test.trx")
      tmm.save(trx, trx_path)
      trx.close()

      loaded = tmm.load(trx_path)
      print("_uncompressed_folder_handle:", loaded._uncompressed_folder_handle)
      # we have None here  <-- bug: should be a TemporaryDirectory handle

      # close() silently skips ALL mmap cleanup.
      loaded.close()

      print("streamlines._data type:", type(loaded.streamlines._data))
      # After close(), _data should be gone — but close() only called __init__()
      # without first releasing the np.memmap handles. 
      # so on windows, file is locked, on linux, mmap is never released so leak

I will dig a bit more to see if it was on purpose and there is something that I am doing wrong.

feedback are welcome @frheault and @arokem

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions