vert.x icon indicating copy to clipboard operation
vert.x copied to clipboard

WebClient.rxSendStream(file) cause AsyncFile OutOfMemoryError

Open qizhengyu opened this issue 8 years ago • 21 comments

I'm using WebClient.rxSendStream method to upload a large file(2GB) to remote server. The upload speed is normal in the beginning. But it became slower and slower, finally the OutOfMemoryError was thrown.

Vert.x version 3.5.0

Below is my code causing problem, I can consistently reproduce it:

Vertx vertx = Vertx.vertx();
WebClient client = WebClient.create(vertx, new WebClientOptions()
               .setDefaultHost("localhost")
               .setDefaultPort(3000));

vertx.fileSystem().rxOpen("largefile.tar", new OpenOptions())
               .flatMap(file ->client.put("/upload/largefile.tar").rxSendStream(file))
               .subscribe(resp -> {
                   System.out.println("response received: " + resp.statusCode());
               });

qizhengyu avatar Jan 19 '18 14:01 qizhengyu

Error log here:

Jan 19, 2018 10:24:28 AM io.vertx.core.impl.ContextImpl
SEVERE: Unhandled exception
java.lang.OutOfMemoryError: Java heap space
	at io.netty.util.internal.PlatformDependent.allocateUninitializedArray(PlatformDependent.java:194)
	at io.netty.buffer.UnpooledUnsafeHeapByteBuf.allocateArray(UnpooledUnsafeHeapByteBuf.java:34)
	at io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf.allocateArray(UnpooledByteBufAllocator.java:127)
	at io.netty.buffer.UnpooledHeapByteBuf.<init>(UnpooledHeapByteBuf.java:58)
	at io.netty.buffer.UnpooledUnsafeHeapByteBuf.<init>(UnpooledUnsafeHeapByteBuf.java:29)
	at io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf.<init>(UnpooledByteBufAllocator.java:122)
	at io.netty.buffer.UnpooledByteBufAllocator.newHeapBuffer(UnpooledByteBufAllocator.java:64)
	at io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:162)
	at io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:153)
	at io.netty.buffer.CompositeByteBuf.allocBuffer(CompositeByteBuf.java:1650)
	at io.netty.buffer.CompositeByteBuf.consolidateIfNeeded(CompositeByteBuf.java:405)
	at io.netty.buffer.CompositeByteBuf.addComponent(CompositeByteBuf.java:196)
	at io.vertx.core.http.impl.HttpClientRequestImpl.write(HttpClientRequestImpl.java:847)
	at io.vertx.core.http.impl.HttpClientRequestImpl.write(HttpClientRequestImpl.java:226)
	at io.vertx.core.http.impl.HttpClientRequestImpl.write(HttpClientRequestImpl.java:51)
	at io.vertx.core.streams.impl.PumpImpl.lambda$new$1(PumpImpl.java:64)
	at io.vertx.core.streams.impl.PumpImpl$$Lambda$54/871955780.handle(Unknown Source)
	at io.vertx.core.file.impl.AsyncFileImpl.handleData(AsyncFileImpl.java:352)
	at io.vertx.core.file.impl.AsyncFileImpl.lambda$doRead$2(AsyncFileImpl.java:337)
	at io.vertx.core.file.impl.AsyncFileImpl$$Lambda$58/324620132.handle(Unknown Source)
	at io.vertx.core.file.impl.AsyncFileImpl$2.lambda$done$0(AsyncFileImpl.java:430)
	at io.vertx.core.file.impl.AsyncFileImpl$2$$Lambda$59/332967197.handle(Unknown Source)
	at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:344)
	at io.vertx.core.impl.ContextImpl$$Lambda$47/1128787959.run(Unknown Source)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
	at java.lang.Thread.run(Thread.java:748)

Jan 19, 2018 10:26:27 AM io.vertx.core.impl.ContextImpl
SEVERE: Unhandled exception
rx.exceptions.OnErrorNotImplementedException: Connection was closed
error occured: 
	at rx.functions.Actions$NotImplemented.call(Actions.java:576)
	at rx.functions.Actions$NotImplemented.call(Actions.java:572)
	at rx.Single$11.onError(Single.java:1794)
	at rx.internal.operators.SingleDoOnEvent$SingleDoOnEventSubscriber.onError(SingleDoOnEvent.java:76)
	at io.vertx.rx.java.SingleOnSubscribeAdapter.lambda$call$0(SingleOnSubscribeAdapter.java:33)
	at io.vertx.rxjava.ext.web.client.HttpRequest$1.handle(HttpRequest.java:263)
	at io.vertx.rxjava.ext.web.client.HttpRequest$1.handle(HttpRequest.java:258)
	at io.vertx.ext.web.client.impl.HttpContext.lambda$sendRequest$6(HttpContext.java:159)
	at io.vertx.core.impl.FutureImpl.tryFail(FutureImpl.java:170)
	at io.vertx.ext.web.client.impl.HttpContext.lambda$sendRequest$8(HttpContext.java:218)
	at io.vertx.core.http.impl.HttpClientRequestBase.handleException(HttpClientRequestBase.java:136)
	at io.vertx.core.http.impl.HttpClientRequestImpl.handleException(HttpClientRequestImpl.java:51)
	at io.vertx.core.http.impl.ClientConnection.handleClosed(ClientConnection.java:425)
	at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:342)
	at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:200)
	at io.vertx.core.net.impl.VertxHandler.channelInactive(VertxHandler.java:134)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)
	at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelInactive(CombinedChannelDuplexHandler.java:420)
	at io.netty.handler.codec.ByteToMessageDecoder.channelInputClosed(ByteToMessageDecoder.java:377)
	at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:342)
	at io.netty.handler.codec.http.HttpClientCodec$Decoder.channelInactive(HttpClientCodec.java:281)
	at io.netty.channel.CombinedChannelDuplexHandler.channelInactive(CombinedChannelDuplexHandler.java:223)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1354)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)
	at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:917)
	at io.netty.channel.AbstractChannel$AbstractUnsafe$7.run(AbstractChannel.java:763)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
	at java.lang.Thread.run(Thread.java:748)
Caused by: io.vertx.core.VertxException: Connection was closed
	at io.vertx.core.http.impl.ClientConnection.handleClosed(ClientConnection.java:406)
	... 24 more
`

qizhengyu avatar Jan 19 '18 15:01 qizhengyu

what if you don't use the rx version ?

vietj avatar Jan 19 '18 15:01 vietj

@vietj Still run out of memory, see my code below:

vertx.fileSystem().open(fileToSend, new OpenOptions(), file-> {
                httpRequest.sendStream(file.result(), handler -> {
                    System.out.println("response received");
                });
            });

qizhengyu avatar Jan 19 '18 16:01 qizhengyu

what is the size of your file ?

vietj avatar Jan 22 '18 10:01 vietj

this happens because the default option for WebClientOptions is to follow redirect which can buffer the size you are sending.

you should disable follow redirect on the HttpRequest when the size you are uploading is very large

vietj avatar Jan 22 '18 11:01 vietj

as this is not really safe, I believe we should enable follow redirect by default only for methods that don't send an entity.

vietj avatar Jan 22 '18 11:01 vietj

@pmlopes any opinion on this ?

vietj avatar Jan 22 '18 11:01 vietj

@vietj I'm sending a 2GB file. Thank you for your explanation! It worked by doing.setFollowRedirects(false).

Please feel free to close this issue.

qizhengyu avatar Jan 22 '18 14:01 qizhengyu

I'm keeping it, as I would like to rework this option to avoid it when data is sent, or at least print a warning or something else

vietj avatar Jan 22 '18 14:01 vietj

maybe set a limit, I don't know

vietj avatar Jan 22 '18 15:01 vietj

one way could be to have Flag to be a class and extend CountDownLatch

vietj avatar Jan 22 '18 16:01 vietj

@vietj I guess this happens when you have a non-restful server doing Post/Redirect/Get?

I believe we should enable follow redirect by default only for methods that don't send an entity.

Apache HTTP client's default strategy does this. They say the spec (which one?) recommends this behavior.

tsegismont avatar Jan 23 '18 10:01 tsegismont

redirect is acceptable for PUT

vietj avatar Jan 23 '18 11:01 vietj

@vietj wdym? Isn't the issue the same for post/put?

tsegismont avatar Jan 23 '18 14:01 tsegismont

yes also for post, actually the issue is with the sendStream(ReadStream, Handler) that takes an arbitrary long stream, whatever is the HTTP method.

vietj avatar Jan 23 '18 16:01 vietj

and I meant that in the HTTP spec, doing a redirect on an HTTP put is ok-ish

vietj avatar Jan 23 '18 16:01 vietj

however redirect after post is something else

vietj avatar Jan 23 '18 16:01 vietj

this should be in vertx-web actually

vietj avatar Jan 29 '18 15:01 vietj

going to move it

vietj avatar Jan 29 '18 15:01 vietj

see https://github.com/vert-x3/vertx-web/issues/827

vietj avatar Jan 29 '18 15:01 vietj