com.sun.net.httpserver.HttpExchange 属性损坏了吗?

Are com.sun.net.httpserver.HttpExchange attributes broken?

首先是一些(非常基本的)示例代码来说明我的问题:

final java.util.concurrent.atomic.AtomicLong previousId = new java.util.concurrent.atomic.AtomicLong();
final com.sun.net.httpserver.HttpServer server = com.sun.net.httpserver.HttpServer.create(new java.net.InetSocketAddress(8666), 0);
server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
server.createContext("/", exchange -> {
  final long id = previousId.incrementAndGet();
  System.out.println(String.format("#%s: id attribute=%s", id, exchange.getAttribute("id")));
  exchange.setAttribute("id", id);
  while (true) {
    try {
      Thread.sleep(1_000);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      return;
    }
    System.out.println(String.format("#%s: id attribute=%s", id, exchange.getAttribute("id")));
  }
});
server.start();

虽然 运行 这个程序(在 Java SE 1.8.0_171 上),如果我连续两次向 http://localhost:8666 发出请求,我会得到以下日志:

#1: id attribute=null
#1: id attribute=1
#1: id attribute=1
#2: id attribute=1
#1: id attribute=2
#2: id attribute=2
#1: id attribute=2
#2: id attribute=2

对于请求 #1,属性最初是 null,正如预期的那样。但是对于请求 #2,该属性最初是 1,这似乎暗示初始值是从其他请求继承的。更糟糕的是,如您所见,将请求 #2 的值更改为 2 也会将请求 #1 的值更改为 2。似乎属性在 HttpExchange 个实例之间共享,这不是我所期望的。

我是不是遗漏了什么或者 HttpExchange 属性损坏了?

我认为查看 the source code, it’s perfectly clear that HttpExchange attributes are shared by all the instances on the same HttpContext. I find it misleading though that setAttribute and getAttribute methods are defined on HttpExchange. The Javadoc 也无济于事:

Filter modules may store arbitrary objects with HttpExchange instances as an out-of-band communication mechanism.

我有 sent an email to the net-dev mailing list and filed a bug。我会在收到消息后立即更新此答案。