Bug 237130 - SQLiteDatabase::open should return early if journal mode cannot be set
Summary: SQLiteDatabase::open should return early if journal mode cannot be set
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Sihui Liu
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-02-23 22:36 PST by Ben Nham
Modified: 2022-03-04 00:44 PST (History)
7 users (show)

See Also:


Attachments
Patch (2.01 KB, patch)
2022-02-23 22:40 PST, Ben Nham
no flags Details | Formatted Diff | Diff
Patch (9.45 KB, patch)
2022-03-02 16:00 PST, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (9.53 KB, patch)
2022-03-03 12:08 PST, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch for landing (8.36 KB, patch)
2022-03-03 23:19 PST, Sihui Liu
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ben Nham 2022-02-23 22:36:05 PST
SQLiteDatabase should bail out if opening fails
Comment 1 Ben Nham 2022-02-23 22:40:53 PST
Created attachment 453076 [details]
Patch
Comment 2 Ben Nham 2022-02-23 22:41:50 PST
<rdar://83130954>
Comment 3 Sihui Liu 2022-02-23 23:13:11 PST
Comment on attachment 453076 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=453076&action=review

> Source/WebCore/platform/sql/SQLiteDatabase.cpp:145
> +    if (!isOpen()) {

Why not return earlier? E.g. after sqlite3_open_v2() call above?
Comment 4 Sihui Liu 2022-03-02 16:00:58 PST
Created attachment 453671 [details]
Patch
Comment 5 Sihui Liu 2022-03-03 12:08:15 PST
Created attachment 453774 [details]
Patch
Comment 6 Darin Adler 2022-03-03 16:34:08 PST
Comment on attachment 453774 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=453774&action=review

> Source/WebCore/platform/sql/SQLiteDatabase.cpp:132
> +        int result = SQLITE_OK;

Just because there are braces below doesn’t mean we need to initialize this here. I suppose it’s OK.

> Source/WebCore/platform/sql/SQLiteDatabase.h:102
> +    int checkpoint(CheckpointMode);

Why add this return value? No caller is checking it. Is it for future use?

> Source/WebCore/platform/sql/SQLiteDatabase.h:172
> +    int useWALJournalMode();

Seems like returning an error code as an int with no comment or type to disambiguate and say what the return value means is not a great pattern.

In this particular case, the caller just needs to know if this succeeded or failed, so a bool for success would work and I think might be better than returning the error code.

I suppose it’s not a *disaster* to return an SQLite error code as an int, but how would I know that’s what this function returns? Elsewhere we use named types whenever possible for error codes, or enums or special-purpose objects. I think booleans for success work too.
Comment 7 Sihui Liu 2022-03-03 17:04:12 PST
Comment on attachment 453774 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=453774&action=review

>> Source/WebCore/platform/sql/SQLiteDatabase.h:102
>> +    int checkpoint(CheckpointMode);
> 
> Why add this return value? No caller is checking it. Is it for future use?

I was planned to use the return value to decide if error is lethal and we should close database immediately. 
Then I decided to just keep existing behavior that ignores the checkpoint error. So this is unused; I will revert this change.

>> Source/WebCore/platform/sql/SQLiteDatabase.h:172
>> +    int useWALJournalMode();
> 
> Seems like returning an error code as an int with no comment or type to disambiguate and say what the return value means is not a great pattern.
> 
> In this particular case, the caller just needs to know if this succeeded or failed, so a bool for success would work and I think might be better than returning the error code.
> 
> I suppose it’s not a *disaster* to return an SQLite error code as an int, but how would I know that’s what this function returns? Elsewhere we use named types whenever possible for error codes, or enums or special-purpose objects. I think booleans for success work too.

Sure, will use bool instead. We can get the error with sqlite3_errcode.
Comment 8 Ben Nham 2022-03-03 19:40:25 PST
Patch looks reasonable to me. Just for the future, here is what we found:

1. sqlite3_open_v2 can succeed even if you can't open or create the -shm file for whatever reason (e.g. out of disk space or file handles).
2. Executing `PRAGMA journal_mode=wal` seems to fail if you can't open or create the -shm file. But we were ignoring the result of this operation.
3. Then we would try to call sqlite3_wal_checkpoint_v2 on the connection. This actually crashes if you can't open the -shm file.

So the workaround in this patch was to add a bailout at (2) rather than crashing at (3).
Comment 9 Sihui Liu 2022-03-03 23:19:41 PST
Created attachment 453817 [details]
Patch for landing
Comment 10 EWS 2022-03-04 00:43:59 PST
Committed r290822 (248058@main): <https://commits.webkit.org/248058@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 453817 [details].