Skip to content

respondOutputStream behind nginx fails #1933

@lamba92

Description

@lamba92

Versions: JVM OpenJDK 11
OS: Dockerized Ubuntu ARM64, Raspberry Pi 4
Ktor: 1.3.2
Kotlin: 1.3.72

Code here, docker-compose here, nginx conf here.

when using call.respond() it works fine. Why call.respondOutputStream() fails?

EDIT: After digging in Nginx debug logs it appears that:

So the upstream server returns a response with "Transfer-Encoding: chunked", and the first byte of the response body is 0x89. This character is outside of the ASCII range and clearly is not a hexadecimal number as required by the ​chunked encoding.
Clearly this is an incorrect response from the backend. Depending on how the framework you are using is expected to work, this may either indicate a bug in the framework, or you are using the framework incorrectly.

Cross reference ticket from Nginx issue board.


Nginx error:

upstream sent invalid chunked response while reading upstream

Error stack:

ERROR ktor.application - 200 OK: (request error: java.lang.IllegalStateException: servletRequest.method must not be null)
java.lang.IndexOutOfBoundsException: null
	at javax.imageio.stream.FileCacheImageOutputStream.seek(FileCacheImageOutputStream.java:170)
	at javax.imageio.stream.FileCacheImageOutputStream.close(FileCacheImageOutputStream.java:231)
	at javax.imageio.ImageIO.write(ImageIO.java:1590)
...
Suppressed: java.io.IOException: io.ktor.util.cio.ChannelWriteException: Failed to write to servlet async stream
		at io.ktor.utils.io.jvm.javaio.OutputAdapter.close(Blocking.kt:122)
		at kotlin.io.CloseableKt.closeFinally(Closeable.kt:56)
		... 8 common frames omitted
	Caused by: io.ktor.util.cio.ChannelWriteException: Failed to write to servlet async stream
		at io.ktor.server.servlet.ServletWriter.wrapException(ServletWriter.kt:119)
		at io.ktor.server.servlet.ServletWriter.onError(ServletWriter.kt:112)
		at io.ktor.server.servlet.ServletWriter.run(ServletWriter.kt:52)
		at io.ktor.server.servlet.ServletWriter$run$1.invokeSuspend(ServletWriter.kt)
		at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
...
Caused by: org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe
		at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:309)
		at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:272)
		at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:118)
		at io.ktor.server.servlet.ServletWriter.loop(ServletWriter.kt:87)
		at io.ktor.server.servlet.ServletWriter$loop$1.invokeSuspend(ServletWriter.kt)
		... 46 common frames omitted
	Caused by: java.io.IOException: Broken pipe
		at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
		at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
		at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
		at sun.nio.ch.IOUtil.write(IOUtil.java:65)
		at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:468)
		at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:138)
...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions