Skip to content

Add a guard check against stack overflow when initializing BuildData#486

Merged
nikita-tkachenko-datadog merged 7 commits into
masterfrom
nikita-tkachenko/build-data-init-cycle
Jan 7, 2025
Merged

Add a guard check against stack overflow when initializing BuildData#486
nikita-tkachenko-datadog merged 7 commits into
masterfrom
nikita-tkachenko/build-data-init-cycle

Conversation

@nikita-tkachenko-datadog

@nikita-tkachenko-datadog nikita-tkachenko-datadog commented Jan 3, 2025

Copy link
Copy Markdown
Collaborator

Requirements for Contributing to this repository

  • Fill out the template below. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
  • The pull request must only fix one issue at the time.
  • The pull request must update the test suite to demonstrate the changed functionality.
  • After you create the pull request, all status checks must be pass before a maintainer reviews your contribution. For more details, please see CONTRIBUTING.

What does this PR do?

Adds a workaround for a BuildData initialization issue.

Some of the fields in this BuildData are initialized with the values obtained from a Run instance.
Querying Run fields can trigger run initialization in some cases (e.g. loading run data from disk when resuming a run after Jenkins restart).
During run initialization some logs are written.
Writing logs, in turn, requires creating a TaskListener associated with the run.
Creating the listener triggers initialization of DatadogTaskListenerDecorator, as the listener's logger needs to be decorated.
The decorator creates another BuildData instance, whose creation starts the whole cycle from the beginning.
As the result, the code enters an endless cycle of initializing BuildData while initializing BuildData, which terminates with a stack overflow.

The added code checks that BuildData for the provided run is already being created in the current thread.
If that is the case, empty BuildData instance is returned for the nested calls in order to break the cycle.
As a side effect, whatever logs are written while Run instance is being initialized will not be tagged with that run's data.

It should fix the following issues:
#484
#389

Description of the Change

Alternate Designs

Possible Drawbacks

Verification Process

Additional Notes

Release Notes

Review checklist (to be filled by reviewers)

  • Feature or bug fix MUST have appropriate tests (unit, integration, etc...)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have one changelog/ label attached. If applicable it should have the backward-incompatible label attached.
  • PR should not have do-not-merge/ label attached.
  • If Applicable, issue must have kind/ and severity/ labels attached at least.

@nikita-tkachenko-datadog nikita-tkachenko-datadog added the changelog/Fixed Fixed features results into a bug fix version bump label Jan 3, 2025
@nikita-tkachenko-datadog
nikita-tkachenko-datadog marked this pull request as ready for review January 4, 2025 09:53
if (!BUILD_DATA_BEING_CREATED.get().add(run)) {
String runName = run != null ? run.getDisplayName() : null;
DatadogUtilities.severe(LOGGER, null, "BuildData creation is in progress for run " + runName + "; using empty data");
return BuildData.EMPTY;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Although we return EMPTY here, we know that there's a call in the stack that will return the populated data? Is my assumption correct?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, exactly.
In short, the problem is that constructing a BuildData instance may trigger some run state to be initialised, and this results in Jenkins printing some logs to the console (something like "reading state for run #N from disk").
The plugin wants to send these logs to Datadog, just like any other logs. And it wants to tag each of these logs with the data of the associated run. But that requires a BuildData, so we enter an endless cycle.
By returning EMPTY we break this cycle. The downside is that logs triggered by BuildData init will miss the run info (they will only be tagged with the common Jenkins instance info).

drodriguezhdez
drodriguezhdez previously approved these changes Jan 7, 2025
@nikita-tkachenko-datadog
nikita-tkachenko-datadog merged commit f6fe4c9 into master Jan 7, 2025
@nikita-tkachenko-datadog
nikita-tkachenko-datadog deleted the nikita-tkachenko/build-data-init-cycle branch January 7, 2025 11:38
@edwinmlobo

Copy link
Copy Markdown

Thanks for fix @nikita-tkachenko-datadog
When would you be releasing this version ?

@nikita-tkachenko-datadog

Copy link
Copy Markdown
Collaborator Author

Thanks for fix @nikita-tkachenko-datadog When would you be releasing this version ?

Hi @edwinmlobo, plugin v9.0.0 with the fix has just been released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/Fixed Fixed features results into a bug fix version bump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants