Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AutoContinuingInputStream failure case #429

Merged

Conversation

tjcelaya
Copy link
Contributor

@tjcelaya tjcelaya commented Aug 2, 2018

Currently if an unrecoverable exception happens while using try-with-resources in combination with auto-resume the following will occur:

  • AutoContinuingInputStream will catch it and invoke attemptRecovery to ask its continuator for a continuation
  • the continuator will rethrow the exception because it is considered fatal
  • immediately ContinuingInputStream will call this.discardWrapped() which closes the wrapped stream and sets this.wrapped to null
  • the catch in attemptRecovery will attempt to add the newly caught exception as a suppressed exception of the original (thinking it was related to recovery)
  • try-with-resources sees the original exception and calls close on the outer stream which triggers an NPE when AutoContinuingInputStream calls this.getWrapped().close() (because the superclass already discarded the wrapped stream so there is nothing to close) so that NPE is attached to the original exception as a suppressed exception

This is all much more complicated than it needs to be and is the result of over-eager behavior on the part of ContinuingInputStream and a missing null check in AutoContinuingInputStream#close. The resulting exception looks like this:

java.lang.IllegalArgumentException: Self-suppression not permitted
	at java.lang.Throwable.addSuppressed(Throwable.java:1043)
	at com.joyent.manta.util.AutoContinuingInputStream.attemptRecovery(AutoContinuingInputStream.java:59)
	at com.joyent.manta.util.AutoContinuingInputStream.read(AutoContinuingInputStream.java:81)
	at com.joyent.manta.client.MantaObjectInputStream.read(MantaObjectInputStream.java:175)
	...
	at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:721)
	at com.joyent.manta.client.ApacheHttpGetResponseEntityContentContinuatorIT.regularObjectDownloadUnencrypted(ApacheHttpGetResponseEntityContentContinuatorIT.java:244)
	Suppressed: java.lang.NullPointerException
		at com.joyent.manta.util.AutoContinuingInputStream.close(AutoContinuingInputStream.java:138)
		at com.joyent.manta.org.apache.commons.io.IOUtils.closeQuietly(IOUtils.java:339)
		at com.joyent.manta.org.apache.commons.io.IOUtils.closeQuietly(IOUtils.java:270)
		at com.joyent.manta.client.MantaObjectInputStream.close(MantaObjectInputStream.java:190)
		at com.joyent.manta.client.ApacheHttpGetResponseEntityContentContinuatorIT.$closeResource(ApacheHttpGetResponseEntityContentContinuatorIT.java:231)
		at com.joyent.manta.client.ApacheHttpGetResponseEntityContentContinuatorIT.regularObjectDownloadUnencrypted(ApacheHttpGetResponseEntityContentContinuatorIT.java:245)
		... 30 more
Caused by: javax.net.ssl.SSLException: SSL peer shut down incorrectly
	at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:596)
	...
	at com.joyent.manta.org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:148)
	at com.joyent.manta.util.ContinuingInputStream.read(ContinuingInputStream.java:146)
	at com.joyent.manta.util.AutoContinuingInputStream.read(AutoContinuingInputStream.java:79)
	... 37 more

Instead, we'd rather just throw javax.net.ssl.SSLException: SSL peer shut down incorrectly and call it a day. This PR fixes that situation and adds a test for verifying that an exception rethrown by InputStreamContinuator#buildContinuation is rethrown as-is.

This PR also makes ContinuingInputStream easier to use by removing the automatic calls to discardWrapped() on any exception so some redundant try statements have also been removed.

Copy link
Contributor

@dekobon dekobon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

bytesRead,
ex);
ex.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any possibility of ex being null?

} catch (final IOException ce) {
originalIOException.addSuppressed(ce);
super.continueWith(this.continuator.buildContinuation(originalIOException, this.getBytesRead()));
} catch (final IOException ioe) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be paranoid, but you may want to catch UncheckedIOException as well.

@tjcelaya tjcelaya merged commit 882bbac into TritonDataCenter:master Aug 3, 2018
@tjcelaya tjcelaya deleted the fix/continuator-self-suppression branch August 3, 2018 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants