Grant Application - CoW Protocol Settlement Execution Race Fix

Grant Title

Settlement Execution Lifecycle Correctness: Fixing the Race Condition in CoW Protocol Services

Target issue: CoW Protocol Issue #5031

Authors

Applicant: Trac

Target repository: GitHub - cowprotocol/services: Off-chain services for CoW Protocol · GitHub

Experiences and Qualifications

I have experience working with Rust, asynchronous programming, PostgreSQL, and SQLx. These technologies are relevant to investigating concurrent execution paths, database write ordering, and persistence consistency.

My approach to this contribution is to establish the existing lifecycle invariants before changing the code, reproduce the failure deterministically, and implement the smallest correction that preserves the current architecture.

I will document the investigation findings and provide the regression tests, implementation, and validation evidence in a focused pull request. I will not represent unrelated project work as a prior contribution to CoW Protocol.

Grant Description

Problem

CoW Protocol Issue #5031 concerns a race condition in the settlement execution persistence lifecycle.

The issue describes a situation where recording the end of a settlement execution can race ahead of inserting its start record. If the completion operation updates a row that does not yet exist, the update may affect zero rows. If the start insert subsequently succeeds, the persisted execution may lack the expected terminal outcome.

The relevant sequence is:

  1. A settlement execution starts and schedules the operation that records its start.
  2. The completion path runs before the start record has been committed.
  3. The completion update finds no matching record.
  4. The start record is inserted successfully afterward.
  5. The persisted execution history may no longer reflect the actual terminal outcome.

This can leave execution records incomplete and potentially contribute to orders remaining in an in-flight state longer than intended.

The key issue is lifecycle correctness: asynchronous persistence operations must not silently lose a terminal execution outcome because they ran in an unexpected order.

Proposed solution

I propose a focused investigation and correction within the existing CoW Protocol services repository.

The work will first establish the current behavior, schema constraints, and relevant error-handling paths. I will then reproduce the race in a deterministic regression test and implement the smallest safe correction consistent with the existing lifecycle.

The implementation will be selected only after verifying the current code. Depending on the existing design, the correction may involve coordinating the start-record operation with completion persistence, handling missing-record updates explicitly, or introducing an appropriate recovery mechanism.

I will not assume that a particular solution is correct before inspecting the current implementation. The fix must not fabricate required fields or introduce invalid partial records to accommodate an end-first write.

Technical investigation

The investigation will cover:

  • The code paths responsible for recording settlement execution starts and terminal outcomes.
  • The ordering and completion guarantees of the relevant asynchronous tasks.
  • Database operations, transaction boundaries, schema constraints, and affected-row handling.
  • Error propagation, retries, task cancellation, and missing-record behavior where relevant.
  • Downstream handling of incomplete execution records and in-flight orders.

The investigation will use the CoW Protocol services repository and the target Issue #5031.

Related references include:

These references provide context for the execution lifecycle. Their relationship to the reported race will be verified during the investigation rather than assumed.

Regression testing and validation

The implementation will include deterministic regression coverage for the identified failure mode.

Tests will cover:

  • The normal start-then-completion sequence.
  • The problematic completion-before-start ordering.
  • Database failures and missing-record behavior relevant to the selected solution.
  • Task failure or cancellation paths where applicable.
  • Consistency of persisted terminal outcomes.
  • Preservation of normal successful execution behavior.

The test will use controlled synchronization or suitable test hooks rather than arbitrary sleeps.

I will run the relevant repository tests, formatting checks, and lint commands, recording the actual results in the pull request. I will not claim a test has passed until it has been executed successfully.

Expected outcomes and ecosystem impact

The intended outcomes are:

  • More reliable persistence of settlement execution lifecycle events.
  • Reduced risk of losing terminal outcomes because of asynchronous write ordering.
  • Clearer and more consistent execution records for debugging and operational investigation.
  • Regression coverage that helps prevent the same failure from returning.
  • A small, maintainable correction that fits the existing services architecture.

The contribution is intentionally limited to a specific correctness issue. It does not propose rebuilding settlement execution, introducing new infrastructure, or redesigning the broader execution lifecycle.

The impact will be demonstrated through the regression test, implementation review, and validation evidence rather than unsupported claims about production incident reductions.

Type of Grant

Milestone-based technical contribution.

Funding will be requested against two defined milestones, with acceptance based on the agreed deliverables and evidence of completion.

Milestones

Milestone Title Due date Funding request
1 Investigation and deterministic regression test End of week 1 $500
2 Minimal correction, validation, and pull request End of week 2 $1,500
Total 2 weeks $2,000

The schedule begins upon grant approval and confirmation of the commencement date. Milestone completion will be reported in the original CoW Grants Program forum thread.

Specifics: Milestone 1 — Investigation and Regression Test

Funding request: $500

Deadline: End of week 1 after commencement.

Tasks and deliverables:

  1. Review Issue #5031 and confirm its current status.
  2. Inspect the relevant execution lifecycle, asynchronous task coordination, persistence operations, and database schema.
  3. Determine whether the race remains reproducible against the current codebase.
  4. Develop a deterministic regression test demonstrating the failure where reproducible.
  5. Document the lifecycle invariant that the correction must preserve.
  6. Establish a technically safe implementation approach and consult maintainers where necessary.

Acceptance criteria:

  • The investigation findings are documented.
  • The race is reproduced where possible, with the test demonstrating the failure.
  • The proposed correction respects the existing data model and lifecycle invariants.
  • The next implementation step is agreed upon based on the evidence.

If the issue has already been resolved or cannot be reproduced, I will document the findings and consult the maintainers before proceeding. I will not invent a failure or implement an unjustified change to satisfy the milestone.

Significance: This milestone establishes that the funded work addresses a real, current problem and that the proposed correction is grounded in the actual codebase.

Specifics: Milestone 2 — Implementation, Tests, and Pull Request

Funding request: $1,500

Deadline: End of week 2 after commencement.

Tasks and deliverables:

  1. Implement the agreed minimal correction.
  2. Add regression coverage for the identified race and relevant failure paths.
  3. Verify that terminal outcomes are persisted consistently.
  4. Run the applicable repository test suite, formatting, and lint checks.
  5. Document the implementation decisions and actual validation results.
  6. Submit a focused pull request to GitHub - cowprotocol/services: Off-chain services for CoW Protocol · GitHub .
  7. Respond to reasonable technical review feedback within the agreed scope.

Acceptance criteria:

  • The implementation addresses the verified failure mode.
  • Regression tests pass.
  • Normal execution behavior is preserved.
  • Relevant repository checks and their results are documented.
  • A review-ready pull request contains the implementation, tests, and validation evidence.

Significance: This milestone delivers the actual correctness fix and leaves maintainers with a reproducible test and a focused, reviewable contribution.

Payment acceptance will be based on the agreed deliverables and review process, rather than solely on upstream merge timing, which may depend on factors outside the applicant’s control.

Length

Estimated duration: Two weeks.

Commencement: Upon successful grant approval and confirmation of the commencement date.

Expected completion: Fourteen days after commencement.

Week 1 covers the investigation, reproduction, regression test, and agreement on the implementation approach. Week 2 covers the correction, additional testing, validation, and pull request.

The schedule assumes access to the relevant development environment and reasonable availability of maintainers for questions about intended lifecycle behavior. Any need to change the scope or timeline will be discussed before undertaking additional work.

Funding Request

Total funding requested: $2,000 equivalent in xDAI on Gnosis Chain.

COW tokens requested: None.

The funding allocation is:

  • $500: Investigation, lifecycle analysis, reproduction, and deterministic regression testing.
  • $1,500: Implementation, additional tests, validation, documentation, and review-driven revisions.

The amount reflects a narrowly scoped, two-week technical contribution involving asynchronous execution analysis, database persistence correctness, regression testing, and implementation within an existing production codebase.

The larger allocation is assigned to the implementation milestone because it includes the correction, validation, and preparation of a review-ready pull request.

The grant does not fund unrelated refactoring, a general architectural redesign, or new infrastructure.

Final disbursement will follow the approved grant terms and the Grants Committee’s applicable currency conversion and payment procedures.

Gnosis Chain Address (to receive the grant)

Wallet address: 0xde4d7146fb31c75535d675f40b033aa047f4e3ef

Other Information

Scope boundaries

Included:

  • Investigation of Issue #5031.
  • Analysis of asynchronous execution and persistence behavior.
  • Deterministic regression testing.
  • A minimal correction for the verified race.
  • Relevant failure-path tests and validation.
  • A focused pull request and technical documentation.

Excluded:

  • Rebuilding settlement execution or the autopilot.
  • A general redesign of execution persistence.
  • New monitoring or testing infrastructure.
  • Unrelated race-condition fixes.
  • Broad schema migrations unless demonstrably necessary and agreed with maintainers.
  • Unrelated refactoring or product features.

Risks and mitigation

The issue has already been fixed. I will verify the current issue and code status before implementation and consult maintainers if the original work is no longer needed.

The race cannot be reproduced. I will document the investigation and avoid implementing a speculative correction.

The proposed correction violates database invariants. I will inspect the existing schema and lifecycle requirements and avoid fabricated fields or invalid partial records.

The test depends on timing. I will use controlled synchronization or suitable test hooks rather than arbitrary sleeps.

Upstream review takes longer than expected. I will deliver the agreed code, tests, validation evidence, and pull request. Acceptance will follow the approved milestone terms rather than depend exclusively on an upstream merge.

External references

Terms and Conditions

By submitting this grant application, I acknowledge and agree to be bound by the CoW DAO Participation Agreement and the CoW DAO Grant Agreement Terms.

Hi @Trac, thanks for the application.

After discussing this internally, we’ve decided not to move forward. Our reasoning: the core team runs the autopilot, and issue #5031 hasn’t been labeled for outside work, which the services repo’s contribution guide treats as core team only until a maintainer says otherwise. That makes it the core team’s call, so we wouldn’t fund it through a grant.

Appreciate the effort you put into this and wish you the best with your other work.