• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19625

07 Jan 2025 06:25PM UTC coverage: 88.546% (+0.002%) from 88.544%
#19625

push

github

ejona86
netty: Fix getAttributes() data races in NettyClientTransportTest

Since approximately the LBv2 API (the current API) was introduced, gRPC
won't use a transport until it is ready. Long ago, transports could be
used before they were ready and these old tests were not waiting for the
negotiator to complete before starting. We need them to wait for the
handshake to complete to avoid a test-only data race in getAttributes()
noticed by TSAN.

Throwing away data frames in the Noop handshaker is necessary to act
like a normal handshaker; they don't allow data frames to pass until the
handshake is complete. Without the handling, it goes through invalid
code paths in NettyClientHandler where a terminated transport becomes
ready, and a similar data race.

```
  Write of size 4 at 0x00008db31e2c by thread T37:
    #0 io.grpc.netty.NettyClientHandler.handleProtocolNegotiationCompleted(Lio/grpc/Attributes;Lio/grpc/InternalChannelz$Security;)V NettyClientHandler.java:517
    #1 io.grpc.netty.ProtocolNegotiators$GrpcNegotiationHandler.userEventTriggered(Lio/netty/channel/ChannelHandlerContext;Ljava/lang/Object;)V ProtocolNegotiators.java:937
    #2 io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(Ljava/lang/Object;)V AbstractChannelHandlerContext.java:398
    #3 io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(Lio/netty/channel/AbstractChannelHandlerContext;Ljava/lang/Object;)V AbstractChannelHandlerContext.java:376
    #4 io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(Ljava/lang/Object;)Lio/netty/channel/ChannelHandlerContext; AbstractChannelHandlerContext.java:368
    #5 io.grpc.netty.ProtocolNegotiators$ProtocolNegotiationHandler.fireProtocolNegotiationEvent(Lio/netty/channel/ChannelHandlerContext;)V ProtocolNegotiators.java:1107
    #6 io.grpc.netty.ProtocolNegotiators$WaitUntilActiveHandler.channelActive(Lio/netty/channel/ChannelHandlerContext;)V ProtocolNegotiators.java:1011
    ...

  Previo... (continued)

33676 of 38032 relevant lines covered (88.55%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.43
/../netty/src/main/java/io/grpc/netty/ClientTransportLifecycleManager.java
1
/*
2
 * Copyright 2016 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.netty;
18

19
import com.google.errorprone.annotations.CanIgnoreReturnValue;
20
import io.grpc.Attributes;
21
import io.grpc.Status;
22
import io.grpc.internal.ManagedClientTransport;
23

24
/** Maintainer of transport lifecycle status. */
25
final class ClientTransportLifecycleManager {
26
  private final ManagedClientTransport.Listener listener;
27
  private boolean transportReady;
28
  private boolean transportShutdown;
29
  private boolean transportInUse;
30
  /** null iff !transportShutdown. */
31
  private Status shutdownStatus;
32
  /** null iff !transportShutdown. */
33
  private Throwable shutdownThrowable;
34
  private boolean transportTerminated;
35

36
  public ClientTransportLifecycleManager(ManagedClientTransport.Listener listener) {
1✔
37
    this.listener = listener;
1✔
38
  }
1✔
39

40
  public Attributes filterAttributes(Attributes attributes) {
41
    if (transportReady || transportShutdown) {
1✔
42
      return attributes;
×
43
    }
44
    return listener.filterTransport(attributes);
1✔
45
  }
46

47
  public void notifyReady() {
48
    if (transportReady || transportShutdown) {
1✔
49
      return;
×
50
    }
51
    transportReady = true;
1✔
52
    listener.transportReady();
1✔
53
  }
1✔
54

55
  /**
56
   * Marks transport as shutdown, but does not set the error status. This must eventually be
57
   * followed by a call to notifyShutdown.
58
   */
59
  public void notifyGracefulShutdown(Status s) {
60
    if (transportShutdown) {
1✔
61
      return;
1✔
62
    }
63
    transportShutdown = true;
1✔
64
    listener.transportShutdown(s);
1✔
65
  }
1✔
66

67
  /** Returns {@code true} if was the first shutdown. */
68
  @CanIgnoreReturnValue
69
  public boolean notifyShutdown(Status s) {
70
    notifyGracefulShutdown(s);
1✔
71
    if (shutdownStatus != null) {
1✔
72
      return false;
1✔
73
    }
74
    shutdownStatus = s;
1✔
75
    shutdownThrowable = s.asException();
1✔
76
    return true;
1✔
77
  }
78

79
  public void notifyInUse(boolean inUse) {
80
    if (inUse == transportInUse) {
1✔
81
      return;
×
82
    }
83
    transportInUse = inUse;
1✔
84
    listener.transportInUse(inUse);
1✔
85
  }
1✔
86

87
  public void notifyTerminated(Status s) {
88
    if (transportTerminated) {
1✔
89
      return;
1✔
90
    }
91
    transportTerminated = true;
1✔
92
    notifyShutdown(s);
1✔
93
    listener.transportTerminated();
1✔
94
  }
1✔
95

96
  public Status getShutdownStatus() {
97
    return shutdownStatus;
1✔
98
  }
99

100
  public Throwable getShutdownThrowable() {
101
    return shutdownThrowable;
1✔
102
  }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc