Skip to content

fix: code panics on very long task names - #731

Merged
jorbaum merged 2 commits into
cloudfoundry:mainfrom
nouseforaname:fix/truncate-string-to-avoid-panic-for-long-task-names
Sep 28, 2026
Merged

jorbaum merged 2 commits into
cloudfoundry:mainfrom
nouseforaname:fix/truncate-string-to-avoid-panic-for-long-task-names

Conversation

@nouseforaname

Copy link
Copy Markdown
Contributor

while most of the values have been truncated, it seems that task names ( including their APP/TASK/ prefix ) are not currently truncated.

e.g. given a sourceType value with length >= 126 and a sourceInstance != "", the previous state of the code would panic when trying to truncate sourceInstance (assuming sourceInstance is the reason we need to truncate):

if len(sourceType)+len(sourceInstance)+3 > 128 {
  sourceInstance = sourceInstance[:(128 - len(sourceType) - 3)]
}

for every sourceType >= 126 the above code will produce a panic.

this tries to lose the least amount of info to keep identification of log sources possible.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Testing performed?

  • Unit tests

Checklist:

  • This PR is being made against the main branch, or relevant version branch
  • I have added testing for my changes

@jorbaum jorbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this. Looks almost ready. I found things worthy addressing.

Comment thread src/pkg/egress/syslog/tcp.go
Comment thread src/pkg/egress/syslog/rfc5424_test.go Outdated
Comment thread src/pkg/egress/syslog/tcp.go Outdated
@nouseforaname
nouseforaname force-pushed the fix/truncate-string-to-avoid-panic-for-long-task-names branch from d7e9064 to 82eab9d Compare September 10, 2026 10:39
@nouseforaname

Copy link
Copy Markdown
Contributor Author

Thanks for fixing this. Looks almost ready. I found things worthy addressing.

Yes you did. An I thought of a different way of approaching this. It now calculates a ratio and tries to trim both values with the minimal amount of chars while keeping as much info as possible.

@jorbaum jorbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this so quickly and adding such a good amount of new tests :) .

I found one regression for sourceInstance == "" and one case where I am quite certain it still panics right now.

Its close to being ready. Please also keep in mind that this code is on a critical path for log forwarding and needs to be performant as in some environment we need to process a lot of logs. But I think the current code is fine as is in that respect. Please think about making it simpler by avoiding floating-point rounding complexity though. But again: it is fine as is.

Comment thread src/pkg/egress/syslog/rfc5424_test.go Outdated
Comment thread src/pkg/egress/syslog/tcp.go Outdated
Comment thread src/pkg/egress/syslog/tcp.go Outdated
Comment thread src/pkg/egress/syslog/rfc5424_test.go Outdated
@nouseforaname
nouseforaname force-pushed the fix/truncate-string-to-avoid-panic-for-long-task-names branch 2 times, most recently from 73c3790 to 16f84db Compare September 17, 2026 10:49
Comment thread src/pkg/egress/syslog/tcp.go Outdated
Comment thread src/pkg/egress/syslog/tcp.go Outdated
Comment thread src/pkg/egress/syslog/tcp.go Outdated
@nouseforaname
nouseforaname force-pushed the fix/truncate-string-to-avoid-panic-for-long-task-names branch 2 times, most recently from 109d784 to 2fd9a8d Compare September 18, 2026 14:53
while most of the values have been truncated, it seems that task names
( including their `APP/TASK/` prefix ) are not currently truncated.

RFC5424 PROCID is limited to 128 bytes, but a valid CF task name
(including its APP/TASK/ prefix) can exceed it. The old code sliced
sourceInstance with a negative bound
(`sourceInstance[:(128 - len(sourceType) - 3)]`), panicking the syslog
agent and disrupting delivery to configured drains.

It seems that similar issues could occur on deployments utilizing the
agent given very long VM Names.

This attempts to keep as much info as possible for the `/index` part of
the templating. We assume that these values should contain either numerical
indexes (app/task instances) or a UUID (vm) identifier.

This attempts a truncated rendered PROCID while keeping maximum
log-source identification info:
- cut only when [sourceType/sourceInstance] doesn't fit in 128 bytes
- cap sourceInstance at max 36 chars to keep UUID info and avoid
  issues for unexpected long values in sourceInstance
- sourceType is only trimmed if the result of the above is still too long

Fixes cloudfoundry#730
@nouseforaname
nouseforaname force-pushed the fix/truncate-string-to-avoid-panic-for-long-task-names branch from 2fd9a8d to 4cbaacb Compare September 18, 2026 14:54

@jorbaum jorbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I think this is ready for merging as is. I got one last idea to not always truncate instance ID to 36 chars.


maxReturnLen--

if len(sourceInstance)+len(sourceType) > maxReturnLen {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OPTIONAL: When len(sourceType)+len(sourceInstance) > 125 we now hard-cap sourceInstance to 36 chars even when sourceType is tiny. Think about truncating sourceType first, so we would have the same behavior from before even for edge cases like:

sourceType = "CELL" (4 chars), sourceInstance = a 123-char composite identifier
org-payments-prod.space-checkout-eu.app-fraud-scoring-v2.instance-9b1deb4d-…-2b0d7b3dcb6d.zone-eu10-az3-canary

┌──────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────────────────────────┐
│          │                                               rendered PROCID                                               │             len              │
├──────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────┤
│ Previous │ [CELL/org-payments-prod.space-checkout-eu.app-fraud-scoring-v2.instance-9b1deb4d-…dcb6d.zone-eu10-az3-cana] │ 128 — keeps 121 of 123 chars │
├──────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────┤
│ Current  │ [CELL/org-payments-prod.space-checkout-eu.]                                                                 │ 43 — hard-capped to 36 chars │
└──────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────┘

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I don't think that's how this will be populated?

I think sourceType will contain CELL/THE-CELL_NAME while sourceIndex will be /UUID or the /0

at least that's what my understanding of the code is currently. sourceInstance seems to contain the index ( numerical/uuid) but sourceType will contain the task/appname/vmname

@chombium chombium Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorbaum

OPTIONAL: When len(sourceType)+len(sourceInstance) > 125 we now hard-cap sourceInstance to 36 chars even when sourceType is tiny. Think about truncating sourceType first, so we would have the same behavior from before even for edge cases like

We don't have to do some complex maths when we truncating the value. Whatever we do, we will never know how someone generates the strings. I would rather document how the transaction works and let the users suffer a bit and notice that they are doing some weird things.

IMO: Simple truncate from behind is all that we need

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nouseforaname

but I don't think that's how this will be populated?

Yes. This is just me being overly cautious as both are a free-text string in the end even though they SHOULD follow the format you mentioned. Its just a hypothetical example I made up.

jorbaum
jorbaum previously approved these changes Sep 23, 2026

@jorbaum jorbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/pkg/egress/syslog/tcp.go Outdated
@github-project-automation github-project-automation Bot moved this from Inbox to Pending Merge | Prioritized in Application Runtime Platform Working Group Sep 23, 2026

@chombium chombium left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nouseforaname, @jorbaum keep up the good work 💪

LGTM

@jorbaum
jorbaum merged commit da09cd5 into cloudfoundry:main Sep 28, 2026
6 checks passed
anita-flegg pushed a commit to cloudfoundry/docs-dev-guide that referenced this pull request Sep 28, 2026
The newest syslog-release brings new truncation behaviour in syslog
agent. See
cloudfoundry/loggregator-agent-release#731
chombium pushed a commit that referenced this pull request Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants