Skip to content

[host_modules]: Fix crash loop in procdockerstatsd and Python 3.13 SyntaxWarnings - #425

Open
karthikbabu-CLS wants to merge 1 commit into
sonic-net:masterfrom
karthikbabu-CLS:user/karthikbabu/syntax-warning-procdockerstatsd-fix
Open

[host_modules]: Fix crash loop in procdockerstatsd and Python 3.13 SyntaxWarnings#425
karthikbabu-CLS wants to merge 1 commit into
sonic-net:masterfrom
karthikbabu-CLS:user/karthikbabu/syntax-warning-procdockerstatsd-fix

Conversation

@karthikbabu-CLS

@karthikbabu-CLS karthikbabu-CLS commented Aug 22, 2026

Copy link
Copy Markdown

What I did

  1. Fixed invalid escape sequence \/ in showtech.py regex by using raw string prefix (r'...').
  2. Fixed invalid escape sequence \P in hostcfgd sed replacement patterns (lines 951, 958) by using raw string prefix (r"...").
  3. Wrapped procdockerstatsd main polling loop body in try/except to catch transient errors at INFO and unexpected exceptions at WARNING with traceback, preventing daemon crash loop.

Why I did it

  1. Python 3.12+ raises SyntaxWarning: invalid escape sequence for \/ and \P in non-raw strings. These warnings appear in syslog on every boot.
  2. procdockerstatsd crashes when a monitored Docker process exits mid-poll (raises FileNotFoundError or psutil.Error). The crash causes supervisor to restart the daemon, which then crashes again on the next poll cycle — creating a crash loop. The try/except lets the daemon log the transient error at INFO and continue the next cycle instead of crashing.

How I verified it

Verified on community SONiC VS image (x86_64-kvm_x86_64-r0):

BEFORE:
INFO python3[679]: /usr/local/lib/python3.13/dist-packages/host_modules/showtech.py:43: SyntaxWarning: invalid escape sequence '/'
INFO hostcfgd[4752]: /usr/local/bin/hostcfgd:951: SyntaxWarning: invalid escape sequence '\P'
INFO hostcfgd[4752]: /usr/local/bin/hostcfgd:958: SyntaxWarning: invalid escape sequence '\P'

AFTER:
admin@sonic:$ show logging | grep "SyntaxWarning"
admin@sonic:
$

Details

No functional change. Regex patterns, sed replacement patterns, and stats collection logic unchanged. Only string prefixes corrected and exception handling added around the existing polling loop.

1. showtech.py: Use raw string for regex pattern to fix invalid escape
   sequence '\/' SyntaxWarning on Python 3.12+.

2. hostcfgd: Use raw string for sed replacement patterns to fix invalid
   escape sequence '\P' SyntaxWarning on Python 3.12+ (lines 951, 958).

3. procdockerstatsd: Wrap main polling loop body in try/except to catch
   transient errors (FileNotFoundError, psutil.Error) at INFO level
   and unexpected exceptions at WARNING with full traceback. Prevents
   daemon crash loop when a monitored process exits mid-poll.

Verified on community SONiC VS image:
Before: 3 SyntaxWarning messages on boot (showtech.py, hostcfgd x2)
After: 0 SyntaxWarning messages
@mssonicbld

Copy link
Copy Markdown

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown

Hi, there are workflow run(s) waiting for approval, you may be first-time contributor. I will notify maintainers to help approve once PR is approved. Thanks!

---Powered by SONiC BuildBot

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.

2 participants