Skip to content

Make HTTP/2 frame hashCode consistent with equals - #16692

Merged
chrisvest merged 1 commit into
netty:4.1from
daguimu:fix/http2-frame-hashcode-equals-13659
Jun 4, 2026
Merged

chrisvest merged 1 commit into
netty:4.1from
daguimu:fix/http2-frame-hashcode-equals-13659

Conversation

@daguimu

@daguimu daguimu commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

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

@daguimu
daguimu force-pushed the fix/http2-frame-hashcode-equals-13659 branch from 97bc9de to 095ff02 Compare April 23, 2026 16:43
@daguimu
daguimu force-pushed the fix/http2-frame-hashcode-equals-13659 branch from 095ff02 to 86759e4 Compare May 11, 2026 09:11
Motivation:

AbstractHttp2StreamFrame.hashCode() returns super.hashCode() - the
per-instance identity hash inherited from Object - whenever its stream
is null. But AbstractHttp2StreamFrame.equals() considers two frames
with a null stream to be equal, so two distinct instances are
equals()-true yet hashCode()-different, violating the Object.hashCode
contract.

DefaultHttp2PingFrame.hashCode() has the same shape: equals() compares
ack and content, but hashCode() seeds its result with super.hashCode()
(Object identity) before folding in ack, so two pings with the same
ack and content hash to different values. Beyond the contract violation,
the field content is not even consumed by the hash, so pings that do
differ only in content collide.

These frames being used as HashMap keys or members of HashSet today
silently break lookups.

Modification:

- AbstractHttp2StreamFrame.hashCode: when stream is null, return 0
  instead of super.hashCode(); a constant matches the equals semantics
  of "null == null".
- DefaultHttp2PingFrame.hashCode: fold content (as Long.hashCode-like
  xor of its halves, matching the convention in DefaultHttp2ResetFrame)
  and ack together; drop the super.hashCode() identity seed.
- Add Http2DefaultFramesTest entries that pin down the contract:
  equal stream frames with null streams share a hashCode, equal pings
  share a hashCode, and pings differing in content or ack do not
  trivially collide.

Result:

HTTP/2 frame types satisfy Object's equals/hashCode contract, making
them safe to use as keys in hashed collections.

Fixes netty#13659
@daguimu
daguimu force-pushed the fix/http2-frame-hashcode-equals-13659 branch from 86759e4 to ed968de Compare May 12, 2026 05:30
@chrisvest chrisvest added this to the 4.1.136.Final milestone Jun 4, 2026
@chrisvest chrisvest added needs-cherry-pick-4.2 This PR should be cherry-picked to 4.2 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Jun 4, 2026
@chrisvest
chrisvest merged commit 13b6e99 into netty:4.1 Jun 4, 2026
22 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #16908

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Jun 4, 2026
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.2: #16910

@github-actions github-actions Bot removed the needs-cherry-pick-4.2 This PR should be cherry-picked to 4.2 once merged. label Jun 4, 2026
chrisvest pushed a commit that referenced this pull request Jun 5, 2026
)

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()`:

```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

Co-authored-by: Guimu <[email protected]>
chrisvest pushed a commit that referenced this pull request Jun 5, 2026
)

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

---
## 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

Co-authored-by: Guimu <[email protected]>
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
## 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 netty#13659
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.

3 participants