Skip to content

[#1013] Add finalize workflow to prevent race conditions - #1052

Draft
Abdelrhmansersawy wants to merge 1 commit into
pgmoneta:mainfrom
Abdelrhmansersawy:workflow_finalize
Draft

Abdelrhmansersawy wants to merge 1 commit into
pgmoneta:mainfrom
Abdelrhmansersawy:workflow_finalize

Conversation

@Abdelrhmansersawy

@Abdelrhmansersawy Abdelrhmansersawy commented Mar 22, 2026 •

Copy link
Copy Markdown
Collaborator

resolved #1013

Changes

  1. Added a Finalize workflow step to guarantee repository locks are released.
  2. Replaced shared global variables with ART nodes to pass data directly and prevent race conditions.
  3. Centralized client responses to ensure they are sent only after cleanup finishes.

@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen
Could you give this a high-level review to check if I’m heading in the right direction?
I’m working on ensuring that the finalizer workflow does not affect the normal behavior of any workflows.

@jesperpedersen jesperpedersen added the enhancement Improvement to an existing feature label Mar 22, 2026
@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy Yes, this is the right direction

Comment thread src/libpgmoneta/wf_finalize.c Resolved
@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author

A few updates:

  1. Backup needs the repo lock held through WAL compress, so finalize can't be a workflow step - make it a utility function callers invoke before the workflow response is sent.
  2. Verify workflow doesn't do a repositry lock, so we don't need to make finialize for it.

@Abdelrhmansersawy Abdelrhmansersawy changed the title [#1013] Add finalize workflow phase for backup, restore, and verify [#1013] Add finalize release for backup and restore Mar 28, 2026
@jesperpedersen

Copy link
Copy Markdown
Member
  1. There is encryption as well... I don't see why we can't set the flag based on a node value in art* in teardown
  2. That sounds like a bug

@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author
  1. That sounds like a bug

No, I mean repo lock release don't happen in pgmoneta_verify but in pgmoneta_sha512_verification.

if (locked)
{
atomic_store(&config->common.servers[server].repository, false);
locked = false;
}
}

For consistent I think I can move this to replace finialize...

@Abdelrhmansersawy
Abdelrhmansersawy force-pushed the workflow_finalize branch 3 times, most recently from 76d013b to bd8292f Compare March 28, 2026 17:59
@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen PTAL

Comment thread src/include/workflow.h Resolved
Comment thread src/include/workflow_funcs.h Resolved
Comment thread src/libpgmoneta/backup.c Resolved
Comment thread src/libpgmoneta/backup.c Resolved
Comment thread src/libpgmoneta/backup.c Resolved
Comment thread src/libpgmoneta/backup.c Resolved
Comment thread src/libpgmoneta/backup.c Resolved
@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy The comments for backup applies for the others as well

@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy I think this depend on #1038 ...
@sommye-ctr Do you agree ?

@Abdelrhmansersawy

Abdelrhmansersawy commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen
Do u see it's better to stop now, return work on it after #1038 is merged?

Or skip wal_compression_encryption part for now (as it's not PR focused) and open new PR later.

@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy Just leave it for now - we will circle back soon as #1038 is close

@sommye-ctr

Copy link
Copy Markdown
Collaborator

Yes I think we can have this on hold till #1038 is settled

@Abdelrhmansersawy
Abdelrhmansersawy force-pushed the workflow_finalize branch 2 times, most recently from 834f84b to 6fd6da8 Compare April 5, 2026 08:09
@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen PTAL

Comment thread src/libpgmoneta/backup.c Resolved
Comment thread src/libpgmoneta/restore.c Resolved
Comment thread src/libpgmoneta/verify.c Resolved
@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy You need to start the backup/restore/verify workflow with the finalizer... Then in each workflow check that you have the lock. And in finalizer's teardown send the recorded result.

There are no loops inside a workflow node - that is handled by workflow.c

@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen

  1. Right now pgmoneta_workflow_execute() doesn’t run teardowns if a setup or execute step fails - it just returns 1. If the finalizer runs first and acquires the lock in its setup, then a later step fails, the lock would never be released. Should I update pgmoneta_workflow_execute() to always run teardowns on error?

  2. About “sending the recorded result in the finalizer’s teardown” - currently the response is built after the workflow finishes. For example in backup.c, things like backup_size are computed after pgmoneta_workflow_execute() returns, and client_fd / payload aren’t stored in the ART nodes. Should I pass these through the nodes so the finalizer can handle it, or did you have something else in mind?

@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy

  1. Yes, we need to change the workflow - we need a way to identify required workflow success (wf_finalizer), so if that fails an INFO (or WARN) is logged. The rest are logged as DEBUG for the backup, restore and verify flow. So, maybe 0, 1, 2... Of course that means that we have make guards in setup/execute/teardown in order to support this
  2. For backup/restore/verify - stick everything you need in the ART and do it in wf_finalizer.c::teardown - remember to make those nodes mandatory as an assert

@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen
If we move finalize to the start of the workflow chain, its teardown will actually run first, since pgmoneta_workflow_execute() processes teardowns in forward order.

That means the repository lock could be released too early - before the rest of the steps finish cleaning up. In that case, a new backup might grab the lock while the previous one is still freeing memory or removing files.

Do you think it would be better to run teardowns in reverse order (LIFO) inside pgmoneta_workflow_execute(), so the lock is always released last?

@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy Yes, it sounds correct to run setup in order, and teardown in reserve. Of course we have to go through the current teardown implementation will work with that

@Abdelrhmansersawy Abdelrhmansersawy changed the title [#1013] Add finalize release for backup and restore [#1013] Add finalize workflow to prevent race conditions Apr 6, 2026
@Abdelrhmansersawy

Abdelrhmansersawy commented Apr 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen PTAL

A few follow-up:

  1. Moved client responses into wf_finalize::teardown - just stick what you need in the ART + mandatory assert.
  2. Added WORKFLOW_RESULT_FATAL and WORKFLOW_RESULT_ERROR so failed workflows log at the right level.
  3. Made sure teardowns always run, even when something fails.
  4. Put the Finalize step at the head of the chain.
  5. Made teardowns run in reverse order of setup - as wf_finalize::teardown run as the last step.
  6. Added a guard in pgmoneta_common_setup to skip if finalize is part of the workflow but the lock was not acquired.

Comment thread src/include/workflow.h Resolved
Comment thread src/libpgmoneta/backup.c
config->common.servers[server].active_backup = false;
atomic_store(&config->common.servers[server].repository, false);

pgmoneta_management_response_error(NULL, client_fd, config->common.servers[server].name,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved, right ?

@Abdelrhmansersawy Abdelrhmansersawy Apr 7, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jesperpedersen
The problem that there are two types of errors that exist:

  1. Early errors - things like invalid server or WAL streaming not enabled. These happen before pgmoneta_workflow_execute runs, so finalize_teardown never gets called. The error response in backup.c is needed here.

  2. Workflow errors - when something fails inside pgmoneta_workflow_execute, it already calls finalize_teardown, which sends the error response. Then we fall through to the same error: label and send it again. So the client gets the response twice.

How should I handle this? I was thinking of checking if NODE_ERROR_CODE is already set to skip the duplicate, but I'm not sure if there's a better way. This also applies to restore.c and verify.c.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The things is that we need to move the logic up to workflow.c such that if there is an existing error code, then the execute isn't called. I think it is ok for all setup/teardown to be called.

finalizer should always send the repsonse.

For now, we have backup, restore and verify - but long term all workflows should use finalizer

Comment thread src/libpgmoneta/restore.c Resolved
Comment thread src/libpgmoneta/restore.c

workflow = pgmoneta_workflow_create(WORKFLOW_TYPE_POST_ROLLUP, tmp_backup);
if (carry_out_workflow(workflow, nodes) != RESTORE_OK)
if (pgmoneta_workflow_execute(workflow, nodes, &en, &ec))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong

Comment thread src/libpgmoneta/restore.c

workflow = pgmoneta_workflow_create(WORKFLOW_TYPE_RESTORE, backup);
if ((ret = carry_out_workflow(workflow, nodes) != RESTORE_OK))
if (pgmoneta_workflow_execute(workflow, nodes, &en, &ec))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this

Comment thread src/libpgmoneta/restore.c
}

if ((ret = carry_out_workflow(workflow, nodes) != RESTORE_OK))
if (pgmoneta_workflow_execute(workflow, nodes, &en, &ec))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jesperpedersen
I will revert pgmoneta_rollup_backups back to carry_out_workflow. But should I update carry_out_workflow to run teardowns in reverse order and also run teardowns when an error happens? Right now it skips teardowns on error and runs them forward.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main thing is that only workflow.c should call those - there might be pre checks inside the execute's that will fail, but that means there is a bug in the previous workflow nodes...

If we need to split the existing nodes up even more then be it

Comment thread src/libpgmoneta/verify.c Resolved
Comment thread src/libpgmoneta/verify.c
}

if (pgmoneta_management_create_response(payload, server, &response))
if (pgmoneta_workflow_execute(workflow, nodes, &en, &ec))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong as well...

Comment thread src/libpgmoneta/verify.c
error:

pgmoneta_delete_directory((char*)pgmoneta_art_search(nodes, NODE_TARGET_BASE));
pgmoneta_management_response_error(ssl, client_fd, config->common.servers[server].name,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong - its finalizer::teardown

@jesperpedersen

Copy link
Copy Markdown
Member

@Abdelrhmansersawy Rebase, please

@Abdelrhmansersawy

Copy link
Copy Markdown
Collaborator Author

@jesperpedersen
About strict reponse on finialize workflow, I see that we can extract pgmoneta_workflow_teardown into a public function to guarantee teardowns always run.

  1. Inside pgmoneta_workflow_execute: It simply calls pgmoneta_workflow_teardown.
  2. On error labels: I removed manual error responses. Workflows now exclusively call pgmoneta_workflow_teardown(workflow, nodes).
    • Early errors (workflow is NULL): pgmoneta_workflow_teardown detects this and creates a temporary finalizer workflow to reliably send the error response.
    • Execution errors: It normally runs the teardown chain to send the response.

WDYT?

@LoayTarek5 LoayTarek5 mentioned this pull request Sep 28, 2026
3 of 7 tasks
@jesperpedersen

Copy link
Copy Markdown
Member

Please, rebase

2 similar comments
@jesperpedersen

Copy link
Copy Markdown
Member

Please, rebase

@jesperpedersen

Copy link
Copy Markdown
Member

Please, rebase

@Pranav-error

Copy link
Copy Markdown
Collaborator

The race from #1013 is still on main (fbc7e6f), so this is still needed. In pgmoneta_backup() (src/libpgmoneta/backup.c) the order is:

  1. L419: pgmoneta_management_response_ok() — the CLI gets its success reply
  2. L426: pgmoneta_wal_server_compress_encrypt(server, NULL, NULL)
  3. L430-432: active_backup = false, repository_pid = 0, repository = false

so a script that starts the next backup as soon as the CLI returns can still get "Server primary is active".

The branch is 299 commits behind main and conflicts in 10 files (workflow.h, workflow_funcs.h, backup.c, restore.c, se_s3.c, se_ssh.c, verify.c, wf_backup.c, wf_restore.c, workflow.c), so it needs a substantial rebase. If that is a lot of work, one option would be to split it: first a small PR for #1013 that sends the response last, after the WAL compress/encrypt and the lock release, then the finalize/teardown rework on top. (Releasing the lock earlier instead would not be safe: retention skips a server while repository is held, retention.c:84, and that is what keeps it from removing WAL during the compress/encrypt step.)

@jesperpedersen

Copy link
Copy Markdown
Member

Please, rebase

2 similar comments
@jesperpedersen

Copy link
Copy Markdown
Member

Please, rebase

@jesperpedersen

Copy link
Copy Markdown
Member

Please, rebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backup race condition: pgmoneta_backup releases repository lock too late

5 participants