-
Notifications
You must be signed in to change notification settings - Fork 353
Fix HTTP/2 stream priority memory leak by dropping unused PriorityValue cache from AbstractH2StreamMultiplexer. #617
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
Open
arturobernalg
wants to merge
1
commit into
apache:master
Choose a base branch
from
arturobernalg:leak_priority
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+59
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,9 +35,7 @@ | |
| import java.util.Deque; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Queue; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentLinkedDeque; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
@@ -141,7 +139,6 @@ enum SettingsHandshake { READY, TRANSMITTED, ACKED } | |
| private EndpointDetails endpointDetails; | ||
| private boolean goAwayReceived; | ||
|
|
||
| private final Map<Integer, PriorityValue> priorities = new ConcurrentHashMap<>(); | ||
| private volatile boolean peerNoRfc7540Priorities; | ||
|
|
||
|
|
||
|
|
@@ -1020,6 +1017,9 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio | |
| throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, "Invalid PRIORITY_UPDATE payload"); | ||
| } | ||
| final int prioritizedId = payload.getInt() & 0x7fffffff; | ||
| if (prioritizedId == 0) { | ||
| throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "PRIORITY_UPDATE stream id is 0"); | ||
| } | ||
| final int len = payload.remaining(); | ||
| final String field; | ||
| if (len > 0) { | ||
|
|
@@ -1029,9 +1029,13 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio | |
| } else { | ||
| field = ""; | ||
| } | ||
| final PriorityValue pv = PriorityParamsParser.parse(field).toValueWithDefaults(); | ||
| priorities.put(prioritizedId, pv); | ||
| requestSessionOutput(); | ||
| final PriorityValue pv = parsePriorityValue(field); | ||
| if (pv != null) { | ||
| final H2Stream prioritizedStream = streams.lookup(prioritizedId); | ||
| if (prioritizedStream != null) { | ||
| prioritizedStream.setPriorityValue(pv); | ||
| } | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
|
@@ -1106,7 +1110,7 @@ private void consumeHeaderFrame(final RawFrame frame, final H2Stream stream) thr | |
| if (streamListener != null) { | ||
| streamListener.onHeaderInput(this, streamId, headers); | ||
| } | ||
| recordPriorityFromHeaders(streamId, headers); | ||
| recordPriorityFromHeaders(stream, headers); | ||
| stream.consumeHeader(headers, frame.isFlagSet(FrameFlag.END_STREAM)); | ||
| } else { | ||
| continuation.copyPayload(payload); | ||
|
|
@@ -1125,7 +1129,7 @@ private void consumeContinuationFrame(final RawFrame frame, final H2Stream strea | |
| if (streamListener != null) { | ||
| streamListener.onHeaderInput(this, streamId, headers); | ||
| } | ||
| recordPriorityFromHeaders(streamId, headers); | ||
| recordPriorityFromHeaders(stream, headers); | ||
| if (continuation.type == FrameType.PUSH_PROMISE.getValue()) { | ||
| stream.consumePromise(headers); | ||
| } else { | ||
|
|
@@ -1378,19 +1382,43 @@ H2Stream createStream(final H2StreamChannel channel, final H2StreamHandler strea | |
| return stream; | ||
| } | ||
|
|
||
| private void recordPriorityFromHeaders(final int streamId, final List<? extends Header> headers) { | ||
| private void recordPriorityFromHeaders(final H2Stream stream, final List<? extends Header> headers) { | ||
| if (headers == null || headers.isEmpty()) { | ||
| return; | ||
| } | ||
| for (final Header h : headers) { | ||
| if (HttpHeaders.PRIORITY.equalsIgnoreCase(h.getName())) { | ||
| final PriorityValue pv = PriorityParamsParser.parse(h.getValue()).toValueWithDefaults(); | ||
| priorities.put(streamId, pv); | ||
| final PriorityValue pv = parsePriorityValue(h); | ||
| if (pv != null) { | ||
| stream.setPriorityValue(pv); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private PriorityValue parsePriorityValue(final Header header) { | ||
| if (header == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| return PriorityParamsParser.parse(header).toValueWithDefaults(); | ||
| } catch (final IllegalArgumentException ignore) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arturobernalg Does the parser throw |
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private PriorityValue parsePriorityValue(final String field) { | ||
| if (field == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| return PriorityParamsParser.parse(field).toValueWithDefaults(); | ||
| } catch (final IllegalArgumentException ignore) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| class H2StreamChannelImpl implements H2StreamChannel { | ||
|
|
||
| private final int id; | ||
|
|
@@ -1438,18 +1466,21 @@ public void submit(final List<Header> headers, final boolean endStream) throws I | |
| return; | ||
| } | ||
| ensureNotClosed(); | ||
| if (peerNoRfc7540Priorities && streams.isSameSide(id)) { | ||
| if (peerNoRfc7540Priorities) { | ||
| for (final Header h : headers) { | ||
| if (HttpHeaders.PRIORITY.equalsIgnoreCase(h.getName())) { | ||
| final byte[] ascii = h.getValue() != null | ||
| ? h.getValue().getBytes(StandardCharsets.US_ASCII) | ||
| : new byte[0]; | ||
|
|
||
| final int sid = id & 0x7fffffff; | ||
| final ByteArrayBuffer b = new ByteArrayBuffer(4 + ascii.length); | ||
| b.append((byte) (id >> 24)); | ||
| b.append((byte) (id >> 16)); | ||
| b.append((byte) (id >> 8)); | ||
| b.append((byte) id); | ||
| b.append((byte) (sid >> 24)); | ||
| b.append((byte) (sid >> 16)); | ||
| b.append((byte) (sid >> 8)); | ||
| b.append((byte) sid); | ||
| b.append(ascii, 0, ascii.length); | ||
|
|
||
| final ByteBuffer pl = ByteBuffer.wrap(b.array(), 0, b.length()); | ||
| final RawFrame priUpd = new RawFrame(FrameType.PRIORITY_UPDATE.getValue(), 0, 0, pl); | ||
| commitFrameInternal(priUpd); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arturobernalg Why are we ignoring
prioritizedIdthat produce no match? Should we at least reject ids that have never been seen?