I noticed that the following code will use the same OutputStream
object (same hash code on debug) if a curl request is ctrl+c, then re-run. Even with different parameters, it uses the same OutputStream
. This causes very odd output obviously.
The original output stream starts to throw NullPointerException
when writing to the OutputStream
because the underlying HttpOutputStream
no longer exists (broken pipe). This is why I think it is odd that a subsequent request would reuse the same OutputStream
object.
Closing the output stream in a finally block fixes the issue. Some examples I've seen around the web don't explicitly close the output stream. Is the reuse of the OutputStream
expected? Does anyone have any ideas on why I'd be seeing this behavior?
@RequestMapping(value= URI_ROOT, method= RequestMethod.POST, produces = {"text/event-stream"})
public StreamingResponseBody methodName(
... params
) {
return new StreamingResponseBody() {
@Override
public void writeTo(OutputStream outputStream) {
try {
... code ...
} catch (Exception e) {
LOGGER.info("Migration thread interrupted.",e);
} finally {
IoUtil.closeSilently(outputStream); // This fixes it.
}
}
};
}
OutputStream
whetherStreamingResponseBody
should be closed? – membersound Dec 11 '18 at 11:36