Skip to content

Logging a Path object using structured logging throws StackOverflowError #44507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

nosan
Copy link
Contributor

@nosan nosan commented Mar 3, 2025

See #44502

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 3, 2025
@nosan nosan changed the title Prevent StackOverflowError when serializing Path using JsonValueWriter Logging a Path object using structured logging throws StackOverflowError Mar 3, 2025
@nosan
Copy link
Contributor Author

nosan commented Mar 3, 2025

Pretty simple test to reproduce the cause of the issue.

        @Test
	void writePathRecursively() {
		writePath(Path.of("a"));
	}

	void writePath(Path path) {
		System.out.println(path.toString());
		path.forEach(this::writePath);
	}
a
a
a
a
a
a
a

java.lang.StackOverflowError
	....
	at org.gradle.internal.io.LinePerThreadBufferingOutputStream.println(LinePerThreadBufferingOutputStream.java:239)
	at org.springframework.boot.json.JsonValueWriterTests.writePath(JsonValueWriterTests.java:264)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.boot.json.JsonValueWriterTests.writePath(JsonValueWriterTests.java:265)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.boot.json.JsonValueWriterTests.writePath(JsonValueWriterTests.java:265)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)

Prior to this commit, serializing `java.nio.file.Path` caused
a StackOverflowError because `Path.iterator()` always returns itself
as the first element of the iterator, which results in a StackOverflowError.

This commit serializes `java.nio.file.Path` as JSON String.

Signed-off-by: Dmytro Nosan <[email protected]>
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 16, 2025
@wilkinsona wilkinsona modified the milestones: 3.4.x, 3.4.5 Apr 16, 2025
wilkinsona pushed a commit that referenced this pull request Apr 16, 2025
Prior to this commit, serializing `java.nio.file.Path` caused
a StackOverflowError because `Path.iterator()` always returns itself
as the first element of the iterator, which results in a
StackOverflowError.

This commit serializes `java.nio.file.Path` as JSON String.

See gh-44507

Signed-off-by: Dmytro Nosan <[email protected]>
wilkinsona added a commit that referenced this pull request Apr 16, 2025
wilkinsona added a commit that referenced this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants