public void trySendError(Throwable t) { final boolean responseWasCommitted; try { synchronized (monitor) { String reasonPhrase = getReasonPhrase(t, developerMode); int statusCode = getStatusCode(t); responseWasCommitted = responseCommitted; if (!responseCommitted) { responseCommitted = true; sendErrorAsync(statusCode, reasonPhrase); } } } catch (Throwable e) { servletOutputStreamWriter.fail(t); return; } //Must be evaluated after state transition for test purposes(See ConformanceTestException) //Done outside the monitor since it causes a callback in tests. if (responseWasCommitted) { RuntimeException exceptionWithStackTrace = new RuntimeException(t); log.log(Level.FINE, "Response already committed, can't change response code", exceptionWithStackTrace); // TODO: should always have failed here, but that breaks test assumptions. Doing soft close instead. //assert !Thread.holdsLock(monitor); //servletOutputStreamWriter.fail(t); servletOutputStreamWriter.close(); } }