grpc/grpc-java · error · NotSerializableException

AnonymousInProcessSocketAddress is not serializable

Error message

AnonymousInProcessSocketAddress is not serializable

What it means

AnonymousInProcessSocketAddress is deliberately non-serializable because it refers to a live in-process server instance that cannot be meaningfully serialized; its writeObject hook throws NotSerializableException to enforce this. It only makes sense within the same JVM.

Solutions

  1. Do not serialize anonymous socket addresses; keep them in-JVM only.
  2. Use a named InProcessSocketAddress(name) (which is serializable by name) if the address must cross boundaries.
  3. Exclude the field from serialization (transient) or store the server's registered name instead.

Example fix

// before
class Context implements Serializable { AnonymousInProcessSocketAddress addr; }
// after
class Context implements Serializable { transient AnonymousInProcessSocketAddress addr; String serverName; }
Defensive patterns

Strategy: type-guard

Validate before calling

if (addr instanceof AnonymousInProcessSocketAddress) throw new IllegalArgumentException("anonymous address cannot be serialized; store the server name instead");

Type guard

boolean isSerializableAddress(SocketAddress a) { return a instanceof InProcessSocketAddress; }

Prevention

When it happens

Trigger: Attempting to serialize an AnonymousInProcessSocketAddress, e.g. passing it through Java serialization, storing it in a serializable object graph, or sending it over a remoting/session mechanism.

Common situations: Putting the server's anonymous listen address into an HttpSession, caching it to disk, or embedding it in a message sent over the wire.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of grpc/grpc-java@64daddc1f3 (2026-09-08). Data as JSON: /api/errors/d49fb9360d5576ac. Report an issue: GitHub.

Appendix: source

Thrown at inprocess/src/main/java/io/grpc/inprocess/AnonymousInProcessSocketAddress.java:43

import java.io.ObjectOutputStream;
import java.net.SocketAddress;
import javax.annotation.Nullable;

/**
 * Custom SocketAddress class for {@link InProcessTransport}, for 
 * a server which can only be referenced via this address instance.
 */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8626")
public final class AnonymousInProcessSocketAddress extends SocketAddress {
  private static final long serialVersionUID = -8567592561863414695L;

  @Nullable
  @GuardedBy("this")
  @SuppressWarnings("serial")
  private InProcessServer server;

  private void writeObject(ObjectOutputStream out) throws IOException {
    throw new NotSerializableException("AnonymousInProcessSocketAddress is not serializable");
  }

  /** Creates a new AnonymousInProcessSocketAddress. */
  public AnonymousInProcessSocketAddress() { }

  @Nullable
  synchronized InProcessServer getServer() {
    return server;
  }

  synchronized void setServer(InProcessServer server) throws IOException {
    if (this.server != null) {
      throw new IOException("Server instance already registered");
    }
    this.server = server;
  }

  synchronized void clearServer(InProcessServer server) {

View on GitHub (pinned to 64daddc1f3)