preface

One day, a colleague from the business side told me that there was a StackOverflowError in the system. Frankly speaking,Exception has seen it, but Error rarely appears, and his mood is like this

A wave of action like a tiger

Let’s start with the bloody crime scene

It is obvious from the exception stack that there is an infinite loop, and stackOverflowErrors are mostly caused by an infinite loop and recursion. So why is this a circular call? We looked at the source code from this overflow crime scene

If you’re using idea, it’ll also prompt you with an icon that will make a recursive call.

So the question is, why do we loop recursively here? Because of this problem, is bound to recur, for the previous view of my source code parsing old fans is simply so easy. We trigger the scene, then hit the breakpoint at the scene of the crime, and then hit the breakpoint again and again, after a few iterations, as shown below

Through the data above, we can already see a rough indication of an endless cycle. We targeted currency and availableCurrencies. Why is there a recurring loop here? Let’s take a look at the source of the Currency class

public static Set<Currency> getAvailableCurrencies(a) {

	/ / to omit...
}
Copy the code

AvailableCurrencies = availableCurrencies = availableCurrencies = availableCurrencies GetAvailableCurrencies () then includes currency, which is in an endless loop.

Local recurrence

To ensure that every fan can participate, Chao draws a minimalist model that must be reproduced. I hope the fans of the public account of Fat North can participate together, instead of every time after watching the analysis, there are people Shouting 666!

public class FeiChaoDTO {

	private Currency currency;

	public Currency getCurrency(a) {
		return currency;
	}

	public void setCurrency(Currency currency) {
		this.currency = currency; }}Copy the code
@Test
public void test(a) throws Exception {
	FeiChaoDTO feiChaoDTO = new FeiChaoDTO();
	feiChaoDTO.setCurrency(Currency.getInstance("CNY"));
	String json = JSON.json(feiChaoDTO);
	System.out.println(json);
}
Copy the code

How to solve

Obviously, the reason for this problem is that the student used the JSON serialization tool class in Dubbo. Because this tool is not the mainstream function of Dubbo, there is not enough attention, naturally there are some imperfect functions, and in the later version of Dubbo2.6.x, it has been marked as expired. In addition, let’s take a look at the Ali development manual

To solve this problem is very simple, since to JSON serialization, then use the mainstream JSON serialization tool, whether using FastJSON, Gson, Jackson have to do the processing of these cycles, or that sentence, this public number has been and the major search engine long-term cooperation, according to my description of the search, such as fastJ Son loop quote, all you want. Of course, careful you may find that these mainstream tools will not serialize availableCurrencies field,Dubbo this serialization tool processing logic and mainstream JSON tool processing is still some different, of course, do not have to struggle,Dubbo itself positioning is RPC framework, like fertilizer to the public number positioning is source solution Analysis, real scene source code combat,JSON analysis we can choose one of the three artifacts above.

Write in the last

Recently, fans and fat toward feedback in the interview process encountered the following questions

All I’m saying is, you don’t have to ask. If you are the fan of fat north, what problem encountered in work and study, welcome to tease up, as long as you take the initiative, our story began!

Fertilizer toward is a focus on principle, source code, development skills of technical public number, number of original thematic source code analysis, real scene source code principle combat (key).Scan the following QR codePay attention to fat, let should build rocket you, no longer screw!