pyocd / pyocd/pyOCD

ResourceWarning for unclosed filestream on svd_data.zip

Open
#1,630 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
560
Avg merge
1h 41m
Merged PRs (30d)
3

Description

Hi.
Installed the latest version today and noticed something I hadn't seen before. Maybe not due to an update but perhaps a change in where the default pack comes from on my setup.

sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='C:\\WorkingDir\\tm_fct_calibration\\.venv\\Lib\\site-packages\\pyocd\\debug\\svd\\svd_data.zip'>
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Looking into it when a board is created as part of a session and the memory map is created a zip stream seems to be made here:

class SVDFile(object):
    @classmethod

    def from_builtin(cls, svd_name):

        try:
            zip_ref = importlib_resources.files("pyocd").joinpath(BUILTIN_SVD_DATA_PATH)
            zip_stream = zip_ref.open('rb')
            zip = zipfile.ZipFile(zip_stream, 'r')
            return SVDFile(zip.open(svd_name))
        except (KeyError, FileNotFoundError, zipfile.BadZipFile) as err:
            from ...core.session import Session
            LOG.warning("unable to open builtin SVD file: %s", err, exc_info=Session.get_current().log_tracebacks)
            return None

But it is not clear to me where this reference is closed so maybe that is what generates the warning. I can see the session is closed when my application exits. My usage is:

with ConnectHelper.session_with_chosen_probe(blocking=False) as session:

            board = session.board
            target = board.target
            flash = target.memory_map.get_boot_memory()

            # Load firmware into device.
            FileProgrammer(session).program(filename)

            # Flash a single phrase without erasing to set the serial number
            flasher = Flash(target, flash.algo)
            flasher.region = flash
            flasher.init(operation=Flash.Operation.PROGRAM)

            flasher.program_phrase(address=0x10001080, bytes=[serial_lsb, serial_msb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])

            # Reset, run.
            target.reset_and_halt()
            target.resume()

Thanks

Ed

Contributor guide

Open the contributing guide

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 SVDFile.from_builtin, where svd_data.zip is opened and the SVD stream is returned, then trace ownership through board creation and session shutdown. Confirm which stream remains open and reproduce the ResourceWarning with the reported session usage; done means the resource is closed without breaking SVD loading.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools, embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.