Fix data race in MessagePack encoder for concurrent server sends (#1486)

This commit is contained in:
Luke Daley
2026-04-04 14:26:16 -07:00
committed by GitHub
parent 58033598c7
commit 8e7eb2bd96
2 changed files with 111 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,10 +41,17 @@ public abstract class AbstractMessagePackEncoder implements MessageEncoder {
@Override
public final void encode(Message msg) throws IOException, ProtocolException {
packer.packArrayHeader(2);
packer.packInt(msg.type().getCode());
encodeMessage(msg);
packer.flush();
// Serialize access to the packer. In pkl server mode the main thread
// (handling CreateEvaluatorRequest) and the executor thread (sending
// EvaluateResponse / ReadModuleRequest) call encode() concurrently.
// Without this lock their writes interleave, corrupting the MessagePack
// stream. See JvmServerTest "concurrent encoding" for a regression test.
synchronized (packer) {
packer.packArrayHeader(2);
packer.packInt(msg.type().getCode());
encodeMessage(msg);
packer.flush();
}
}
protected void packMapHeader(int size, @Nullable Object value1) throws IOException {