mirror of
https://github.com/apple/pkl.git
synced 2026-04-10 18:57:00 +02:00
Fix data race in MessagePack encoder for concurrent server sends (#1486)
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user