← Back to Pull requests

perf(internal): make the Python command check of service name inference linear #22292

Closed
unknown closed this pull request without merging on Dec 02, 2026 15:07 UTC
View on GitHub
unknown unknown commented on Dec 02, 2026 12:36 UTC
Author

Description

APPSEC-72086

PythonDetector, used to infer the base service name from sys.argv, checks whether an argument is a Python executable with (^|/)(?!.\.py$)(python(\d+(\.\d+)?)?$) and re.search. At every / the lookahead scans to the end of the line, so an argument made of many / takes time quadratic in its length.

The lookahead is removed. Wherever the rest of the pattern matches, the text from that point to the end of the line is python with an optional version, which cannot end with .py; and . cannot reach past the end of the line. So the lookahead never rejected a match, and the matches and groups are unchanged. The comment above the pattern now says that, because the argument must end right after python and its optional version, it cannot end with .py.

Customer impact

None: the same arguments are recognized, and the input is the process's own command line. There is no release note, hence the changelog/no-changelog label.

Testing

  • New linear-time test on "/" * 200_000 + ".", bounded at 1 s. It takes about 19 s with the previous regex locally, and 5 ms now.

  • The existing test_python_detector_pattern_matching cases, including python.py and /path/to/python.py, still pass.

  • Not committed: the old and new regexes give the same match span and groups on 400,000 random inputs built from /, python, py, ., digits, .py, newlines and other characters.

  • internal suite, tests/internal/service_name/test_inferred_base_service.py (Python 3.9 and 3.14): 77 passed.

  • scripts/lint style and typing on the changed files.
  • Risks

    None: the output is the same for every argument.

    Additional Notes

    Needed for #22282, which fails on any superlinear regex in ddtrace/.

    🤖 Generated with Claude Code

    [APPSEC-72086]: https://datadoghq.atlassian.net/browse/APPSEC-72086?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 255 lines (+191 / -64) across 3 files Reviewer: dubloom Review duration: 151m
    All Vouch review confidence checks passed
    Residual risk 0.15 = Change Risk (0.39) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 255 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.332
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #22292"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.1% (MEDIUM) · Higher = larger blast radius
    39.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.4% · Safe to merge
    15.4%