Skip to content

fix: release hdc fport so the local port is free after each run - #5

Open
tonycoder-hub wants to merge 2 commits into
masterfrom
cursor/fix-hdc-fport-release-afa7
Open

fix: release hdc fport so the local port is free after each run#5
tonycoder-hub wants to merge 2 commits into
masterfrom
cursor/fix-hdc-fport-release-afa7

Conversation

@tonycoder-hub

Copy link
Copy Markdown
Owner

Fixes codematrixer/hmdriver2#58: after each use the hdc fport mapping was left in place, so the local port stayed occupied.

Cause

hdc fport tcp:<lport> tcp:8012 is opened when HmClient.local_port is first used. The only teardown path was Driver.__del__HmClient.release()hdc fport rm. __del__ is not reliable at process exit, and _rm_local_port() read the local_port property (which opens a new forward if none existed). There was no public close() / context manager / atexit cleanup.

Change

  • Track the allocated local port explicitly; teardown never opens a new fport.
  • Driver.close() / stop() / __enter__/__exit__ / atexit / __del__ all call the same idempotent release that runs hdc fport rm.
  • hdc fport rm uses a 5s timeout so a hung fport rm cannot block process exit.
  • Closing one serial no longer clears every Driver singleton (_instance.clear()).

Related: codematrixer#47

codematrixer/hmdriver2#47 (scripts hang after finish) is partly the same lifecycle. Missing teardown left the forward around (codematrixer#58). When __del__ did run, hdc fport rm could block forever inside _execute_command (also codematrixer#54). The timeout on fport rm addresses that hang; codematrixer#47 may still need follow-up if something other than fport rm is holding the interpreter.

No Harmony device required. Added mocked tests in tests/test_hdc_port_lifecycle.py.

This PR is on the fork only (not opened against upstream). Does not redo codematrixer#59 lxml, codematrixer#60 current_app, codematrixer#61 textContains, codematrixer#62 foldable dump.

Open in Web Open in Cursor 

Signed-off-by: Tony Coder <407243179@qq.com>
Signed-off-by: Tony Coder <407243179@qq.com>
@tonycoder-hub
tonycoder-hub marked this pull request as ready for review August 20, 2026 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

每次使用后端口号不能自动释放

1 participant