From 275ad7dd6fa00bdb145f34f4406146c128fdd80a Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Mon, 27 Jul 2026 12:03:09 -0700 Subject: [PATCH 1/3] api: Add a Supplier overload to Context --- api/src/context/java/io/grpc/Context.java | 17 +++++++++ api/src/test/java/io/grpc/ContextTest.java | 41 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/api/src/context/java/io/grpc/Context.java b/api/src/context/java/io/grpc/Context.java index c19d2db9da3..1f16969e909 100644 --- a/api/src/context/java/io/grpc/Context.java +++ b/api/src/context/java/io/grpc/Context.java @@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; @@ -554,6 +555,22 @@ public V call(Callable c) throws Exception { } } + /** + * Immediately supply a value from a {@link Supplier} with this context as the + * {@link #current} context. + * @param supplier {@link Supplier} to use to produce the value. + * @return result of supplier. + */ + @CanIgnoreReturnValue + public V supply(Supplier supplier) { + Context previous = attach(); + try { + return supplier.get(); + } finally { + detach(previous); + } + } + /** * Wrap a {@link Runnable} so that it executes with this context as the {@link #current} context. */ diff --git a/api/src/test/java/io/grpc/ContextTest.java b/api/src/test/java/io/grpc/ContextTest.java index 7f24ff4461d..f999113d6ea 100644 --- a/api/src/test/java/io/grpc/ContextTest.java +++ b/api/src/test/java/io/grpc/ContextTest.java @@ -49,6 +49,7 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -553,6 +554,46 @@ public Object call() { current.detach(Context.ROOT); } + @Test + public void testSupply() throws Exception { + Context base = Context.current().withValue(PET, "cat"); + Context current = Context.current().withValue(PET, "fish"); + current.attach(); + + final Object ret = new Object(); + Supplier supplier = new Supplier() { + @Override + public Object get() { + runner.run(); + return ret; + } + }; + + assertSame(ret, base.supply(supplier)); + assertSame(base, observed); + assertSame(current, Context.current()); + + assertSame(ret, current.supply(supplier)); + assertSame(current, observed); + assertSame(current, Context.current()); + + final TestError err = new TestError(); + try { + base.supply(new Supplier() { + @Override + public Object get() { + throw err; + } + }); + fail("Excepted exception"); + } catch (TestError ex) { + assertSame(err, ex); + } + assertSame(current, Context.current()); + + current.detach(Context.ROOT); + } + @Test public void currentContextExecutor() { QueuedExecutor queuedExecutor = new QueuedExecutor(); From f7faff8e4878ff4f8aff159299d8227131e87d92 Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Wed, 29 Jul 2026 22:19:55 -0700 Subject: [PATCH 2/3] must use --- api/src/context/java/io/grpc/Context.java | 1 - 1 file changed, 1 deletion(-) diff --git a/api/src/context/java/io/grpc/Context.java b/api/src/context/java/io/grpc/Context.java index 1f16969e909..4ef4f6f6d4f 100644 --- a/api/src/context/java/io/grpc/Context.java +++ b/api/src/context/java/io/grpc/Context.java @@ -561,7 +561,6 @@ public V call(Callable c) throws Exception { * @param supplier {@link Supplier} to use to produce the value. * @return result of supplier. */ - @CanIgnoreReturnValue public V supply(Supplier supplier) { Context previous = attach(); try { From afc76e4875f807be0c92fb7d62cb4c49d97ca133 Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Thu, 30 Jul 2026 10:22:20 -0700 Subject: [PATCH 3/3] Add experimental --- api/src/context/java/io/grpc/Context.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/api/src/context/java/io/grpc/Context.java b/api/src/context/java/io/grpc/Context.java index 4ef4f6f6d4f..b61a082c1b7 100644 --- a/api/src/context/java/io/grpc/Context.java +++ b/api/src/context/java/io/grpc/Context.java @@ -556,9 +556,14 @@ public V call(Callable c) throws Exception { } /** - * Immediately supply a value from a {@link Supplier} with this context as the + * Immediately gets a value from a {@link Supplier} with this context as the * {@link #current} context. + * + *

This API is experimental and + * subject to change. + * * @param supplier {@link Supplier} to use to produce the value. + * @see io.grpc.ExperimentalApi * @return result of supplier. */ public V supply(Supplier supplier) {