fix(server): avoid deadlock during command payload recovery

This commit is contained in:
cabbage committed 2026-10-29 08:34:39 +00:00
1 parent 8d28925e44
commit ef6e65e6f1
4 files changed
+186 -23

No files matched your search

+38 -23
View File
@@ -45,50 +45,65 @@ func checkCommandPayloads(ctx context.Context, database *sql.DB) error {
return err
}
commands, err := database.QueryContext(ctx, `SELECT issue_uuid, execution_spec FROM commands`)
// Join the descriptor and payload in one cursor. The store deliberately has
// one SQLite connection; querying again while this cursor is open deadlocks.
commands, err := database.QueryContext(ctx, `SELECT c.issue_uuid, c.execution_spec,
c.execution_spec_raw_bytes, c.execution_spec_stored_bytes, c.execution_spec_compression,
p.raw_bytes, p.compression, p.sha256, p.inline_data
FROM commands c LEFT JOIN command_payloads p ON p.issue_uuid = c.issue_uuid AND p.kind = 'script'`)
if err != nil {
return err
}
defer commands.Close()
for commands.Next() {
var issue, encoded []byte
if err := commands.Scan(&issue, &encoded); err != nil {
var specRawBytes, specStoredBytes uint64
var specCompression uint32
var rawBytes, compression sql.NullInt64
var digest, stored []byte
if err := commands.Scan(&issue, &encoded, &specRawBytes, &specStoredBytes, &specCompression, &rawBytes, &compression, &digest, &stored); err != nil {
return err
}
if len(issue) != 16 {
if len(issue) != 16 || specStoredBytes != uint64(len(encoded)) {
return ErrCommittedRangeMissing
}
switch specCompression {
case 1:
if specRawBytes != uint64(len(encoded)) || specRawBytes > maxStoredExecutionSpecBytes {
return ErrCommittedRangeMissing
}
case 2:
encoded, err = decompressCommandSpec(encoded, specRawBytes)
if err != nil {
return ErrCommittedRangeMissing
}
default:
return ErrCommittedRangeMissing
}
var spec rvboxv1.ExecutionSpec
if err := proto.Unmarshal(encoded, &spec); err != nil {
// Some low-level store tests intentionally use opaque bytes. Such
// records cannot claim a script source, and dispatch validation will
// reject them before any user code runs.
continue
}
descriptor := spec.GetScript()
var payloadCount uint64
if err := database.QueryRowContext(ctx, `SELECT count(*) FROM command_payloads WHERE issue_uuid = ? AND kind = 'script'`, issue).Scan(&payloadCount); err != nil {
return err
}
if descriptor == nil {
if payloadCount != 0 {
// records cannot own a script payload. Dispatch validation rejects
// their execution spec before any user code runs.
if rawBytes.Valid {
return ErrCommittedRangeMissing
}
continue
}
if payloadCount != 1 || descriptor.GetSizeBytes() > maxStoredScriptBytes || len(descriptor.GetSha256()) != sha256.Size {
descriptor := spec.GetScript()
if descriptor == nil {
if rawBytes.Valid {
return ErrCommittedRangeMissing
}
continue
}
if !rawBytes.Valid || !compression.Valid || descriptor.GetSizeBytes() > maxStoredScriptBytes || len(descriptor.GetSha256()) != sha256.Size {
return ErrCommittedRangeMissing
}
var rawBytes, storedBytes uint64
var compression uint32
var digest, stored []byte
if err := database.QueryRowContext(ctx, `SELECT raw_bytes, stored_bytes, compression, sha256, inline_data FROM command_payloads WHERE issue_uuid = ? AND kind = 'script'`, issue).Scan(&rawBytes, &storedBytes, &compression, &digest, &stored); err != nil {
return err
}
if rawBytes != descriptor.GetSizeBytes() || compression != 2 || len(digest) != sha256.Size || !bytes.Equal(digest, descriptor.GetSha256()) {
if uint64(rawBytes.Int64) != descriptor.GetSizeBytes() || compression.Int64 != 2 || len(digest) != sha256.Size || !bytes.Equal(digest, descriptor.GetSha256()) {
return ErrCommittedRangeMissing
}
decoded, err := decodeCommandPayload(stored, rawBytes, maxStoredScriptBytes)
decoded, err := decodeCommandPayload(stored, uint64(rawBytes.Int64), maxStoredScriptBytes)
computed := sha256.Sum256(decoded)
if err != nil || !bytes.Equal(computed[:], descriptor.GetSha256()) {
return ErrCommittedRangeMissing