Skip to content
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

Logging a Path object using structured logging throws StackOverflowError #44507

Open
wants to merge 1 commit into
base: 3.4.x
Choose a base branch
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants