SpikeInterface / SpikeInterface/spikeinterface

Unclosed file after opening the recording

Open
#3,823 22 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

io
Dominant language
Python
Stars
847
Forks
280
Avg merge
3d 9h
Merged PRs (30d)
29

Description

Hi !

I recently have had an error when opening the binary file :

sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='my\\dat\\file.dat'> 

I used tramalloc to identify where the file was being opened and it is here:

class BinaryRecordingSegment(BaseRecordingSegment):
    def __init__(self, file_path, sampling_frequency, t_start, num_channels, dtype, time_axis, file_offset):
        BaseRecordingSegment.__init__(self, sampling_frequency=sampling_frequency, t_start=t_start)
        self.num_channels = num_channels
        self.dtype = np.dtype(dtype)
        self.file_offset = file_offset
        self.time_axis = time_axis
        self.file_path = file_path
        self.file = open(self.file_path, "rb")
        self.bytes_per_sample = self.num_channels * self.dtype.itemsize
        self.data_size_in_bytes = Path(file_path).stat().st_size - file_offset
        self.num_samples = self.data_size_in_bytes // self.bytes_per_sample

It is opening the file self.file = open(self.file_path, "rb"), but never close it, could it be an issue? I never had this error before so I don't know what led to it. Am I missing something?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at BinaryRecordingSegment.init, especially the open(self.file_path, "rb") call, and trace how the recording segment is used and cleaned up. Reproduce the warning when opening a binary file, then verify that the file is closed at the appropriate lifecycle point without breaking recording access.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.