|
6 | 6 | DatabaseSync, |
7 | 7 | constants, |
8 | 8 | } = require('node:sqlite'); |
9 | | -const { test, suite } = require('node:test'); |
| 9 | +const { it, test, suite } = require('node:test'); |
| 10 | +const dc = require('node:diagnostics_channel'); |
10 | 11 | const { nextDb } = require('../sqlite/next-db.js'); |
11 | 12 | const { Worker } = require('worker_threads'); |
12 | 13 | const { once } = require('events'); |
@@ -652,6 +653,157 @@ test('session[Symbol.dispose]() - after closing database is a no-op', () => { |
652 | 653 | session[Symbol.dispose](); |
653 | 654 | }); |
654 | 655 |
|
| 656 | +// SQLite runs "PRAGMA table_xinfo" from inside its pre-update hook, while it is |
| 657 | +// still walking the connection's session list. Deleting a session from a |
| 658 | +// callback that PRAGMA triggers frees memory the walk is still using, so the |
| 659 | +// close has to be rejected instead. |
| 660 | +suite('session.close() - from a callback', () => { |
| 661 | + const expectedError = |
| 662 | + 'ERR_INVALID_STATE: session cannot be closed while in a callback'; |
| 663 | + |
| 664 | + for (const method of ['close', 'dispose']) { |
| 665 | + const closeSession = (session) => { |
| 666 | + if (method === 'close') { |
| 667 | + session.close(); |
| 668 | + } else { |
| 669 | + session[Symbol.dispose](); |
| 670 | + } |
| 671 | + }; |
| 672 | + |
| 673 | + it(`rejects ${method} from an authorizer callback`, (t) => { |
| 674 | + const database = new DatabaseSync(':memory:'); |
| 675 | + database.exec('CREATE TABLE data(key INTEGER PRIMARY KEY)'); |
| 676 | + const session = database.createSession(); |
| 677 | + let outcome = 'callback did not run'; |
| 678 | + |
| 679 | + database.setAuthorizer((actionCode, param1) => { |
| 680 | + if (actionCode === constants.SQLITE_PRAGMA && param1 === 'table_xinfo') { |
| 681 | + try { |
| 682 | + closeSession(session); |
| 683 | + outcome = 'did not throw'; |
| 684 | + } catch (err) { |
| 685 | + outcome = `${err.code}: ${err.message}`; |
| 686 | + } |
| 687 | + } |
| 688 | + return constants.SQLITE_OK; |
| 689 | + }); |
| 690 | + |
| 691 | + database.exec('INSERT INTO data VALUES (1)'); |
| 692 | + t.assert.strictEqual(outcome, expectedError); |
| 693 | + |
| 694 | + // The session survived and kept recording the insert. |
| 695 | + database.setAuthorizer(null); |
| 696 | + t.assert.notStrictEqual(session.changeset().length, 0); |
| 697 | + session.close(); |
| 698 | + }); |
| 699 | + |
| 700 | + it(`rejects ${method} from a 'sqlite.db.query' subscriber`, (t) => { |
| 701 | + const database = new DatabaseSync(':memory:'); |
| 702 | + database.exec('CREATE TABLE data(key INTEGER PRIMARY KEY)'); |
| 703 | + const session = database.createSession(); |
| 704 | + let outcome = 'callback did not run'; |
| 705 | + |
| 706 | + const handler = ({ sql }) => { |
| 707 | + if (sql.includes('table_xinfo')) { |
| 708 | + try { |
| 709 | + closeSession(session); |
| 710 | + outcome = 'did not throw'; |
| 711 | + } catch (err) { |
| 712 | + outcome = `${err.code}: ${err.message}`; |
| 713 | + } |
| 714 | + } |
| 715 | + }; |
| 716 | + dc.subscribe('sqlite.db.query', handler); |
| 717 | + t.after(() => dc.unsubscribe('sqlite.db.query', handler)); |
| 718 | + |
| 719 | + database.exec('INSERT INTO data VALUES (1)'); |
| 720 | + t.assert.strictEqual(outcome, expectedError); |
| 721 | + |
| 722 | + dc.unsubscribe('sqlite.db.query', handler); |
| 723 | + t.assert.notStrictEqual(session.changeset().length, 0); |
| 724 | + session.close(); |
| 725 | + }); |
| 726 | + |
| 727 | + // Deliberately broader than the crash: the pre-update hook is not on the |
| 728 | + // stack here, so this close is safe today. Node cannot tell whether SQLite |
| 729 | + // is inside that hook, so every callback is rejected. This pins the |
| 730 | + // trade-off rather than leaving it to be discovered as a regression. |
| 731 | + it(`rejects ${method} from a user-defined function`, (t) => { |
| 732 | + const database = new DatabaseSync(':memory:'); |
| 733 | + database.exec('CREATE TABLE data(key INTEGER PRIMARY KEY)'); |
| 734 | + const session = database.createSession(); |
| 735 | + let outcome = 'callback did not run'; |
| 736 | + |
| 737 | + database.function('f', (x) => { |
| 738 | + try { |
| 739 | + closeSession(session); |
| 740 | + outcome = 'did not throw'; |
| 741 | + } catch (err) { |
| 742 | + outcome = `${err.code}: ${err.message}`; |
| 743 | + } |
| 744 | + return x; |
| 745 | + }); |
| 746 | + |
| 747 | + database.exec('SELECT f(1)'); |
| 748 | + t.assert.strictEqual(outcome, expectedError); |
| 749 | + |
| 750 | + // Still closable once the callback is off the stack. |
| 751 | + session.close(); |
| 752 | + t.assert.throws(() => session.close(), { message: 'session is not open' }); |
| 753 | + }); |
| 754 | + } |
| 755 | + |
| 756 | + // Rejecting disposal has a cost: a `using` declaration inside a callback |
| 757 | + // demotes the block's own error to SuppressedError. Accepted for symmetry |
| 758 | + // with StatementSync's disposal, which throws for a busy statement the same |
| 759 | + // way. Pinned here so the trade-off is visible rather than surprising. |
| 760 | + it('demotes a callback error when disposal is rejected', (t) => { |
| 761 | + const database = new DatabaseSync(':memory:'); |
| 762 | + database.exec('CREATE TABLE data(key INTEGER PRIMARY KEY)'); |
| 763 | + let caught; |
| 764 | + |
| 765 | + database.function('f', (x) => { |
| 766 | + try { |
| 767 | + using session = database.createSession(); |
| 768 | + t.assert.ok(session); |
| 769 | + throw new Error('callback error'); |
| 770 | + } catch (err) { |
| 771 | + caught = err; |
| 772 | + } |
| 773 | + return x; |
| 774 | + }); |
| 775 | + |
| 776 | + database.exec('SELECT f(1)'); |
| 777 | + t.assert.ok(caught instanceof SuppressedError); |
| 778 | + t.assert.strictEqual(caught.suppressed.message, 'callback error'); |
| 779 | + t.assert.strictEqual( |
| 780 | + caught.error.message, |
| 781 | + 'session cannot be closed while in a callback', |
| 782 | + ); |
| 783 | + }); |
| 784 | + |
| 785 | + it('leaves an already closed session disposable from a callback', (t) => { |
| 786 | + const database = new DatabaseSync(':memory:'); |
| 787 | + database.exec('CREATE TABLE data(key INTEGER PRIMARY KEY)'); |
| 788 | + const session = database.createSession(); |
| 789 | + session.close(); |
| 790 | + let outcome = 'callback did not run'; |
| 791 | + |
| 792 | + database.setAuthorizer(() => { |
| 793 | + try { |
| 794 | + session[Symbol.dispose](); |
| 795 | + outcome = 'no-op'; |
| 796 | + } catch (err) { |
| 797 | + outcome = `${err.code}: ${err.message}`; |
| 798 | + } |
| 799 | + return constants.SQLITE_OK; |
| 800 | + }); |
| 801 | + |
| 802 | + database.exec('INSERT INTO data VALUES (1)'); |
| 803 | + t.assert.strictEqual(outcome, 'no-op'); |
| 804 | + }); |
| 805 | +}); |
| 806 | + |
655 | 807 | test('session - keeps its database alive after the db handle is dropped', async (t) => { |
656 | 808 | const { gcUntil, onGC } = require('../common/gc'); |
657 | 809 |
|
|
0 commit comments