Skip to content

cats: add missing database locks#1787

Merged
BareosBot merged 24 commits intobareos:masterfrom
sebsura:dev/ssura/master/fix-database-locking
Aug 20, 2024
Merged

cats: add missing database locks#1787
BareosBot merged 24 commits intobareos:masterfrom
sebsura:dev/ssura/master/fix-database-locking

Conversation

@sebsura
Copy link
Contributor

@sebsura sebsura commented Apr 18, 2024

Thank you for contributing to the Bareos Project!

By default, bareos db connections get shared by multiple jobs/threads. It is of utmost importance to ensure that those shared connections only get written to/read from if the calling thread has acquired the lock.

This PR by no means ensures that this is done correctly everywhere, but the start of an ongoing process to weed out every race condition in the database code.

This PR adds some missing locks as well as assertions that locks were taken correctly at the relevant places.
Additionally it renames bac_cursor_ to bar_cursor_.

Some unused database code was also removed as well as making the numeric type count as a numeric type.

Please check

  • Short description and the purpose of this PR is present above this paragraph
  • Your name is present in the AUTHORS file (optional)

If you have any questions or problems, please give a comment in the PR.

Helpful documentation and best practices

Checklist for the reviewer of the PR (will be processed by the Bareos team)

Make sure you check/merge the PR using devtools/pr-tool to have some simple automated checks run and a proper changelog record added.

General
  • Is the PR title usable as CHANGELOG entry?
  • Purpose of the PR is understood
  • Commit descriptions are understandable and well formatted
  • Required backport PRs have been created
Source code quality
  • Source code changes are understandable
  • Variable and function names are meaningful
  • Code comments are correct (logically and spelling)
  • Required documentation changes are present and part of the PR

@sebsura sebsura marked this pull request as draft April 18, 2024 13:28
@sebsura sebsura self-assigned this Apr 29, 2024
@sebsura sebsura force-pushed the dev/ssura/master/fix-database-locking branch from aabaf91 to c83d9a4 Compare April 29, 2024 06:21
@sebsura sebsura marked this pull request as ready for review April 29, 2024 06:21
@sebsura sebsura force-pushed the dev/ssura/master/fix-database-locking branch from c83d9a4 to f438920 Compare April 29, 2024 12:50
@arogge arogge self-requested a review May 14, 2024 09:43
Copy link
Member

@arogge arogge left a comment

Choose a reason for hiding this comment

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

I'm not happy with the amount of calls to AssertOwnership(), especially as it looks pretty expensive. However, I don't have a great idea how to improve it right now.
I'll see if I can come up with an idea over the weekend...

@arogge arogge added this to the 24.0.0 milestone Jun 25, 2024
@sebsura sebsura force-pushed the dev/ssura/master/fix-database-locking branch 2 times, most recently from 92b35fc to 501bfa6 Compare July 9, 2024 07:52
@sebsura
Copy link
Contributor Author

sebsura commented Jul 9, 2024

I had to rebase onto master to make it build again. Sorry!

@sebsura sebsura requested a review from arogge July 9, 2024 08:29
@arogge arogge force-pushed the dev/ssura/master/fix-database-locking branch from 501bfa6 to 122edcb Compare July 25, 2024 08:59
@sebsura sebsura force-pushed the dev/ssura/master/fix-database-locking branch from 7bbc092 to 385b2dc Compare August 14, 2024 08:36
Copy link
Member

@arogge arogge left a comment

Choose a reason for hiding this comment

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

Besides the year in cats.cc's header, I think this is fine now.

Copyright (C) 2011-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2023 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
Copy link
Member

Choose a reason for hiding this comment

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

If we don't have any change, this should also go away (after merging the fixups).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merged the fixups now. Lets see what jenkins has to say.

@sebsura sebsura force-pushed the dev/ssura/master/fix-database-locking branch from 385b2dc to b926bc8 Compare August 20, 2024 12:02
This function is not safe to use if you dont actually own the lock,
but misuse should hopefully lead to a crash anyways.
This is not checked in case of private connections since they are not
shared automatically.  It probably makes sense to also check them
since they might get shared by accident (e.g. setting a pointer in jcr
and then another thread accessing that jcr).
As this is always a pointer into private PQresult data, we should make
sure (at compile time) that we do not write into it.
This also removes the need to AssertOwnership() inside of it
sebsura and others added 15 commits August 20, 2024 13:04
These functions are just used internally, so we make them private.
As this is a private function, it can assume that everything is ok.
Numeric values should also be printed just like other numbers.
db->strerror() should only be called with the lock held unless the db
is private.  90% of the time db calls look like
```
if (!db->Function()) {
   Error(db->strerror());
}
```
In these cases we can need to lock the db during the if, otherwise we
could read stale info (or even run into a race condition as strerror
is unsynchronized).
@BareosBot BareosBot force-pushed the dev/ssura/master/fix-database-locking branch from 49e4899 to a3e0c1d Compare August 20, 2024 13:04
@BareosBot BareosBot merged commit e92206e into bareos:master Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants