GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Auto-port 5.0: Make HTTP/2 frame hashCode consistent with equals by netty-project-bot · Pull Request #16908 · netty/netty

Conversation

@netty-project-bot

@netty-project-bot netty-project-bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Auto-port of #16692 to 5.0
Cherry-picked commit: 13b6e99


Problem

AbstractHttp2StreamFrame and DefaultHttp2PingFrame both violate the Object contract that requires a.equals(b) => a.hashCode() == b.hashCode():

// AbstractHttp2StreamFrame
class C extends AbstractHttp2StreamFrame {
    @Override public String name() { return null; }
}
Object o1 = new C();
Object o2 = new C();
o1.equals(o2);      // true  — both have null stream
o1.hashCode() == o2.hashCode();  // false — identity hash from Object.hashCode()
// DefaultHttp2PingFrame
Object o1 = new DefaultHttp2PingFrame(1, true);
Object o2 = new DefaultHttp2PingFrame(1, true);
o1.equals(o2);      // true  — equal ack and content
o1.hashCode() == o2.hashCode();  // false — hash seeded with identity from Object.hashCode()

Any code that puts these frames into a HashMap/HashSet silently fails to find entries it just inserted.

Root Cause

Both classes call super.hashCode() in paths where no struct­ural hash is available. super.hashCode() resolves to Object.hashCode(), which returns a per-instance identity hash, so it diverges from equals() which compares structural fields (stream for AbstractHttp2StreamFrame; ack and content for DefaultHttp2PingFrame). In the ping frame the problem is compounded: content is not folded into the hash at all, so pings differing only in content also collide.

// AbstractHttp2StreamFrame.hashCode (before)
if (stream == null) {
    return super.hashCode();     // identity hash
}
return stream.hashCode();
// DefaultHttp2PingFrame.hashCode (before)
int hash = super.hashCode();     // identity hash
hash = hash * 31 + (ack ? 1 : 0);
return hash;

Fix

  • AbstractHttp2StreamFrame.hashCode: when stream is null, return 0 (a constant) so two frames whose equals() returns true via null == null also produce the same hash.
  • DefaultHttp2PingFrame.hashCode: fold the full content and ack into the hash. Use the same (int)(v ^ v >>> 32) pattern already used by DefaultHttp2ResetFrame.hashCode to fold the long for Java 8 consistency, then combine with ack via the standard hash * 31 + field pattern.

AbstractHttp2StreamFrame is the base of DefaultHttp2DataFrame, DefaultHttp2ResetFrame, DefaultHttp2HeadersFrame, DefaultHttp2PriorityFrame, and DefaultHttp2WindowUpdateFrame, all of which start their own hashCode with int hash = super.hashCode();. Fixing the base class transitively fixes them: when their stream is null, the base now contributes a deterministic value rather than identity.

Tests Added

Change point Test
AbstractHttp2StreamFrame.hashCode null-stream branch testAbstractHttp2StreamFrameEqualInstancesHaveEqualHashCodes — two anonymous subclass instances with null stream are .equals() true and their hashes match.
DefaultHttp2PingFrame.hashCode contract testDefaultHttp2PingFrameEqualInstancesHaveEqualHashCodes — two pings with the same ack and content are .equals() true and their hashes match.
Regression (sanity) for ping hash testDefaultHttp2PingFrameHashCodeDistinguishesDifferentValues — pings that differ in content or ack do not trivially collide.

All 195 tests in the HTTP/2 frame test set pass locally (0 failures / 0 errors / 2 pre-existing skips). Each failing test was verified to reproduce the contract violation against the pre-fix code (hash mismatch for equal instances).

Impact

  • AbstractHttp2StreamFrame subclasses (DefaultHttp2DataFrame, DefaultHttp2ResetFrame, DefaultHttp2HeadersFrame, DefaultHttp2PriorityFrame, DefaultHttp2WindowUpdateFrame) and DefaultHttp2PingFrame can now be used as keys in hashed collections.
  • The concrete hash values change for instances whose stream is null — any caller that serialized or persisted a hash-code externally would see a difference, but no public API, no equals() semantics, and no wire format changes.

Fixes #13659

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@daguimu @netty-project-bot
Make HTTP/2 frame hashCode consistent with equals (#16692)
3b0ecb9
## Problem

`AbstractHttp2StreamFrame` and `DefaultHttp2PingFrame` both violate the
`Object` contract that requires `a.equals(b) => a.hashCode() ==
b.hashCode()`:

```java
// AbstractHttp2StreamFrame
class C extends AbstractHttp2StreamFrame {
    @OverRide public String name() { return null; }
}
Object o1 = new C();
Object o2 = new C();
o1.equals(o2);      // true  — both have null stream
o1.hashCode() == o2.hashCode();  // false — identity hash from Object.hashCode()
```

```java
// DefaultHttp2PingFrame
Object o1 = new DefaultHttp2PingFrame(1, true);
Object o2 = new DefaultHttp2PingFrame(1, true);
o1.equals(o2);      // true  — equal ack and content
o1.hashCode() == o2.hashCode();  // false — hash seeded with identity from Object.hashCode()
```

Any code that puts these frames into a `HashMap`/`HashSet` silently
fails to find entries it just inserted.

## Root Cause

Both classes call `super.hashCode()` in paths where no struct­ural hash
is available. `super.hashCode()` resolves to `Object.hashCode()`, which
returns a per-instance identity hash, so it diverges from `equals()`
which compares structural fields (stream for `AbstractHttp2StreamFrame`;
ack and content for `DefaultHttp2PingFrame`). In the ping frame the
problem is compounded: `content` is not folded into the hash at all, so
pings differing only in content also collide.

```java
// AbstractHttp2StreamFrame.hashCode (before)
if (stream == null) {
    return super.hashCode();     // identity hash
}
return stream.hashCode();
```

```java
// DefaultHttp2PingFrame.hashCode (before)
int hash = super.hashCode();     // identity hash
hash = hash * 31 + (ack ? 1 : 0);
return hash;
```

## Fix

- `AbstractHttp2StreamFrame.hashCode`: when `stream` is `null`, return
`0` (a constant) so two frames whose `equals()` returns `true` via `null
== null` also produce the same hash.
- `DefaultHttp2PingFrame.hashCode`: fold the full `content` and `ack`
into the hash. Use the same `(int)(v ^ v >>> 32)` pattern already used
by `DefaultHttp2ResetFrame.hashCode` to fold the `long` for Java 8
consistency, then combine with `ack` via the standard `hash * 31 +
field` pattern.

`AbstractHttp2StreamFrame` is the base of `DefaultHttp2DataFrame`,
`DefaultHttp2ResetFrame`, `DefaultHttp2HeadersFrame`,
`DefaultHttp2PriorityFrame`, and `DefaultHttp2WindowUpdateFrame`, all of
which start their own `hashCode` with `int hash = super.hashCode();`.
Fixing the base class transitively fixes them: when their `stream` is
null, the base now contributes a deterministic value rather than
identity.

## Tests Added

| Change point | Test |
|---|---|
| `AbstractHttp2StreamFrame.hashCode` null-stream branch |
`testAbstractHttp2StreamFrameEqualInstancesHaveEqualHashCodes` — two
anonymous subclass instances with null stream are `.equals()` true and
their hashes match. |
| `DefaultHttp2PingFrame.hashCode` contract |
`testDefaultHttp2PingFrameEqualInstancesHaveEqualHashCodes` — two pings
with the same `ack` and `content` are `.equals()` true and their hashes
match. |
| Regression (sanity) for ping hash |
`testDefaultHttp2PingFrameHashCodeDistinguishesDifferentValues` — pings
that differ in `content` or `ack` do not trivially collide. |

All 195 tests in the HTTP/2 frame test set pass locally (0 failures / 0
errors / 2 pre-existing skips). Each failing test was verified to
reproduce the contract violation against the pre-fix code (hash mismatch
for equal instances).

## Impact

- `AbstractHttp2StreamFrame` subclasses (`DefaultHttp2DataFrame`,
`DefaultHttp2ResetFrame`, `DefaultHttp2HeadersFrame`,
`DefaultHttp2PriorityFrame`, `DefaultHttp2WindowUpdateFrame`) and
`DefaultHttp2PingFrame` can now be used as keys in hashed collections.
- The concrete hash values change for instances whose `stream` is `null`
— any caller that serialized or persisted a hash-code externally would
see a difference, but no public API, no `equals()` semantics, and no
wire format changes.

Fixes #13659

(cherry picked from commit 13b6e99)
@netty-project-bot netty-project-bot mentioned this pull request Jun 4, 2026
Make HTTP/2 frame hashCode consistent with equals #16692
Merged
@chrisvest chrisvest added this to the 5.0.0.Final milestone Jun 4, 2026
@chrisvest
chrisvest merged commit 6e2c0eb into 5.0 Jun 5, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest deleted the auto-port-pr-16692-to-5.0 branch June 5, 2026 17:32
@normanmaurer normanmaurer mentioned this pull request Sep 22, 2026
suspend race fix #17588
Closed
@normanmaurer normanmaurer mentioned this pull request Oct 20, 2026
fix iouring zerocopy iov overwrite #17936
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

5.0.0.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@netty-project-bot @chrisvest @daguimu
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.