Skip to content

Commit db658c3

Browse files
committed
Revert the default behavior of the internal duplication of a context to avoid the copy of local context data.
Motivation: Copying the local data of a duplicated context was introduced to duplicate a context and copy its local data in order to implement specific behavior such as the support of grpc context. This behavior is unexpected for some users of this API and we should preserve the previous behavior by default. Changes: - Context duplication does not anymore copy the local data of a context. - A new duplicate(boolean copy) method is added in order to continue support this use case for grpc context implementation
1 parent e486890 commit db658c3

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

vertx-core/src/main/java/io/vertx/core/impl/ContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public Handler<Throwable> exceptionHandler() {
194194
}
195195

196196
@Override
197-
public ContextInternal duplicate() {
197+
public ContextInternal duplicate(boolean copy) {
198198
return new DuplicatedContext(this, locals.length == 0 ? VertxImpl.EMPTY_CONTEXT_LOCALS : new Object[locals.length]);
199199
}
200200
}

vertx-core/src/main/java/io/vertx/core/impl/DuplicatedContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ public boolean isWorkerContext() {
141141
}
142142

143143
@Override
144-
public ContextInternal duplicate() {
144+
public ContextInternal duplicate(boolean copy) {
145145
DuplicatedContext duplicate = new DuplicatedContext(delegate, locals.length == 0 ? VertxImpl.EMPTY_CONTEXT_LOCALS : new Object[locals.length]);
146-
delegate.owner().duplicate(this, duplicate);
146+
if (copy) {
147+
delegate.owner().duplicate(this, duplicate);
148+
}
147149
return duplicate;
148150
}
149151

vertx-core/src/main/java/io/vertx/core/impl/ShadowContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public VertxTracer tracer() {
121121
}
122122

123123
@Override
124-
public ContextInternal duplicate() {
125-
return new ShadowContext(owner, eventLoop, delegate.duplicate());
124+
public ContextInternal duplicate(boolean copy) {
125+
return new ShadowContext(owner, eventLoop, delegate.duplicate(copy));
126126
}
127127

128128
@Override

vertx-core/src/main/java/io/vertx/core/internal/ContextInternal.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,17 @@ default boolean removeLocal(Object key) {
367367
* <p>
368368
* The duplicate context has its own
369369
* <ul>
370-
* <li>local context data</li>
370+
* <li>local context data, initialized with a copy of the existing local context data when {@code copy} is {@code true}</li>
371371
* <li>worker task queue</li>
372372
* </ul>
373373
*
374374
* @return a duplicate of this context
375375
*/
376-
ContextInternal duplicate();
376+
ContextInternal duplicate(boolean copy);
377+
378+
default ContextInternal duplicate() {
379+
return duplicate(false);
380+
}
377381

378382
/**
379383
* Like {@link Vertx#setPeriodic(long, Handler)} except the periodic timer will fire on this context and the

vertx-core/src/test/java/io/vertx/tests/context/ContextTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,13 +1222,16 @@ public void testNestedDuplicate() {
12221222
ctx.putLocal("foo", "bar");
12231223
Object expected = new Object();
12241224
ctx.putLocal(contextLocal, AccessMode.CONCURRENT, expected);
1225-
ContextInternal duplicate = ctx.duplicate();
1225+
ContextInternal duplicate = ctx.duplicate(true);
12261226
assertEquals("bar", duplicate.getLocal("foo"));
12271227
assertEquals(expected, duplicate.getLocal(contextLocal));
12281228
ctx.removeLocal("foo");
12291229
ctx.removeLocal(contextLocal, AccessMode.CONCURRENT);
12301230
assertEquals("bar", duplicate.getLocal("foo"));
12311231
assertEquals(expected, duplicate.getLocal(contextLocal));
1232+
duplicate = ctx.duplicate();
1233+
assertNull(duplicate.getLocal("foo"));
1234+
assertNull(duplicate.getLocal(contextLocal));
12321235
}
12331236

12341237
@Test

0 commit comments

Comments
 (0)