Skip to content

Commit 34e4360

Browse files
committed
Jetty: use blocking methods to send content, due to odd random exception
1 parent 04d2129 commit 34e4360

1 file changed

Lines changed: 42 additions & 30 deletions

File tree

‎modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java‎

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
import static org.eclipse.jetty.http.HttpHeader.SET_COOKIE;
7272
import static org.eclipse.jetty.server.Request.MULTIPART_CONFIG_ELEMENT;
7373

74-
public class JettyContext implements Callback, DefaultContext {
74+
public class JettyContext implements DefaultContext {
75+
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(new byte[0]);
7576
private final int bufferSize;
7677
private final long maxRequestSize;
7778
private Request request;
@@ -377,7 +378,7 @@ public Context setResponseType(@Nonnull MediaType contentType, @Nullable Charset
377378

378379
@Nonnull @Override public Context send(StatusCode statusCode) {
379380
response.setStatus(statusCode.value());
380-
send(ByteBuffer.wrap(new byte[0]));
381+
send(EMPTY_BUFFER);
381382
return this;
382383
}
383384

@@ -401,19 +402,22 @@ public Context setResponseType(@Nonnull MediaType contentType, @Nullable Charset
401402
}
402403

403404
@Nonnull @Override public Context send(@Nonnull ByteBuffer data) {
404-
responseStarted = true;
405-
HttpOutput sender = response.getHttpOutput();
406-
sender.sendContent(data, this);
407-
return this;
405+
try {
406+
responseStarted = true;
407+
HttpOutput sender = response.getHttpOutput();
408+
sender.sendContent(data);
409+
return this;
410+
} catch (IOException x) {
411+
throw SneakyThrows.propagate(x);
412+
} finally {
413+
cleanup();
414+
}
408415
}
409416

410417
@Nonnull @Override public Context send(@Nonnull ReadableByteChannel channel) {
411418
responseStarted = true;
412419
ifSetChunked();
413-
ifStartAsync();
414-
HttpOutput sender = response.getHttpOutput();
415-
sender.sendContent(channel, this);
416-
return this;
420+
return sendStreamInternal(Channels.newInputStream(channel));
417421
}
418422

419423
@Nonnull @Override public Context send(@Nonnull InputStream in) {
@@ -429,8 +433,6 @@ public Context setResponseType(@Nonnull MediaType contentType, @Nullable Charset
429433

430434
private Context sendStreamInternal(@Nonnull InputStream in) {
431435
try {
432-
ifStartAsync();
433-
434436
long len = response.getContentLength();
435437
InputStream stream;
436438
if (len > 0) {
@@ -442,10 +444,12 @@ private Context sendStreamInternal(@Nonnull InputStream in) {
442444
stream = in;
443445
}
444446
responseStarted = true;
445-
response.getHttpOutput().sendContent(stream, this);
447+
response.getHttpOutput().sendContent(stream);
446448
return this;
447449
} catch (IOException x) {
448450
throw SneakyThrows.propagate(x);
451+
} finally {
452+
cleanup();
449453
}
450454
}
451455

@@ -455,6 +459,8 @@ private Context sendStreamInternal(@Nonnull InputStream in) {
455459
return sendStreamInternal(Channels.newInputStream(file));
456460
} catch (IOException x) {
457461
throw SneakyThrows.propagate(x);
462+
} finally {
463+
cleanup();
458464
}
459465
}
460466

@@ -473,18 +479,6 @@ private Context sendStreamInternal(@Nonnull InputStream in) {
473479
return this;
474480
}
475481

476-
@Override public void succeeded() {
477-
complete(null);
478-
}
479-
480-
@Override public void failed(Throwable x) {
481-
complete(x);
482-
}
483-
484-
@Override public InvocationType getInvocationType() {
485-
return InvocationType.NON_BLOCKING;
486-
}
487-
488482
@Override public String toString() {
489483
return getMethod() + " " + pathString();
490484
}
@@ -500,25 +494,43 @@ void complete(Throwable x) {
500494
log.error("exception found while sending response {} {}", getMethod(), pathString(), x);
501495
}
502496
}
497+
498+
clearFiles();
499+
500+
try {
501+
if (request.isAsyncStarted()) {
502+
request.getAsyncContext().complete();
503+
} else {
504+
response.closeOutput();
505+
}
506+
} catch (IOException e) {
507+
log.debug("exception found while closing resources {} {} {}", getMethod(), pathString(), e);
508+
}
509+
}
510+
511+
private void clearFiles() {
503512
if (files != null) {
504513
for (FileUpload file : files) {
505514
try {
506515
file.destroy();
507516
} catch (Exception e) {
508-
log.debug("file upload destroy resulted in exception", e);
517+
router.getLog().debug("file upload destroy resulted in exception", e);
509518
}
510519
}
511520
files.clear();
512521
files = null;
513522
}
523+
}
524+
525+
private void cleanup() {
514526
try {
527+
ifSaveSession();
528+
529+
clearFiles();
530+
} finally {
515531
if (request.isAsyncStarted()) {
516532
request.getAsyncContext().complete();
517-
} else {
518-
response.closeOutput();
519533
}
520-
} catch (IOException e) {
521-
log.debug("exception found while closing resources {} {} {}", getMethod(), pathString(), e);
522534
}
523535
}
524536

0 commit comments

Comments
 (0)