I checked because the response was strange when debugging with Tomcat 8

When I was debugging a web application accessing the server with ajax, the response was strange for some reason. Neither the response body nor the HTTP header is returned as expected. I investigated the cause, so I summarized it.

Execution environment

Test.java


@RestController
public class TestController {

    @RequestMapping(value = "/test.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test(HttpServletRequest request) throws Exception {
        return "{ \"success\" : true }";
    }
}

ajax.js


var req = new XMLHttpRequest();
req.open("POST", "test.do");
req.send();

Status

--Because I was able to break in the test method of TestController, there is no URL error etc. --HTTP status 200 is returned --I also checked Filter and Interceptor, but there is no process that changes the response. Rather, the expected content was stored in the response object. --There is no difference between browsers. Weird response is returned in every browser --Sometimes a normal response is returned

Cause

When I went deeper and deeper in the step execution, an exception was thrown in `` `NioChannel.class``` in tomcat-coyote.jar.

NioChannel.class


/**
 * This method should be used to check the interrupt status before
 * attempting a write.
 *
 * If a thread has been interrupted and the interrupt has not been cleared
 * then an attempt to write to the socket will fail. When this happens the
 * socket is removed from the poller without the socket being selected. This
 * results in a connection limit leak for NIO as the endpoint expects the
 * socket to be selected even in error conditions.
 */
protected void checkInterruptStatus() throws IOException {
    if (Thread.interrupted()) {
         throw new IOException(sm.getString("channel.nio.interrupted"));
    }
}

This exception was caught in the upper layer and rewritten as a response for the error.

Afterword

I had a stack trace of this exception on the console every time, but I was ignoring it because it returned 200. If it looks like an error, you can't ignore it. But I think it's a terrible trap.

Recommended Posts

I checked because the response was strange when debugging with Tomcat 8
I was stuck with the handling of the time zone when formatting with SimpleDateFormat
I checked the number of taxis with Ruby
When creating the top page, I was stuck with the handling of images (super beginner)
When I tried to scroll automatically with JScrollBar, the event handler was drawn only once.
What I was addicted to with the Redmine REST API
I was confused because there was a split in the Array
The story I was addicted to when setting up STS
When I made a bar graph with MPAndroidChart, the x-axis label was misaligned for some reason
What I did when the DB did not start with docker-compose up
What I was addicted to when implementing google authentication with rails
UnsupportedClassVersionError was output when I placed the .war file built with Eclipse on EC2 and executed it.
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
Response when Tomcat server startup fails
Memorandum: What I was addicted to when I hit the accounting freee API
I checked the library "junit-quickcheck" that can perform property-based testing with JUnit
[Java] Java was said to be okay to concatenate strings with +, so I checked
A story I was addicted to when testing the API using MockMVC
Recorded because I was addicted to the standard input of the Scanner class
I was addicted to scrollview because I couldn't tap the variable size UIView
I was addicted to unit testing with the buffer operator in RxJava