-
-
Notifications
You must be signed in to change notification settings - Fork 43
fix: minor improvements (xact callback leak, pg_net.ttl validation)
#278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
utkarash2991
wants to merge
3
commits into
master
Choose a base branch
from
fix/ttl-validation-and-xact-callback
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−5
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import time | ||
| import pytest | ||
| from sqlalchemy import text | ||
| from sqlalchemy.exc import DBAPIError | ||
| from common import collect_response_sync, http_request, restart_worker | ||
| from common import wait_for_response_count, wakeup_worker | ||
|
|
||
|
|
||
| def reset_ttl(autocommit_sess): | ||
| autocommit_sess.execute(text("alter system reset pg_net.ttl")) | ||
| autocommit_sess.execute(text("select pg_reload_conf()")) | ||
|
|
||
|
|
||
| def test_invalid_ttl_is_rejected(autocommit_sess): | ||
| """ | ||
| An invalid interval must be rejected at ALTER SYSTEM time | ||
| instead of making the worker fail when it tries to delete | ||
| expired responses (issue #268) | ||
| """ | ||
| for bad in ["1 blah", "yesterday", "abc", "1 minute 10 potatoes"]: | ||
| with pytest.raises(DBAPIError) as excinfo: | ||
| autocommit_sess.execute(text(f"alter system set pg_net.ttl to '{bad}'")) | ||
| msg = str(excinfo.value) | ||
| assert 'invalid value for parameter "pg_net.ttl"' in msg | ||
| assert "invalid input syntax for type interval" in msg | ||
|
|
||
| # the setting must be untouched | ||
| (ttl,) = autocommit_sess.execute(text("show pg_net.ttl")).fetchone() | ||
| assert ttl == "6 hours" | ||
|
|
||
|
|
||
| def test_negative_ttl_is_rejected(autocommit_sess): | ||
| """ | ||
| A negative ttl would expire every response immediately, reject it | ||
| """ | ||
| for bad in ["-1 hour", "-10 seconds", "1 hour ago"]: | ||
| with pytest.raises(DBAPIError) as excinfo: | ||
| autocommit_sess.execute(text(f"alter system set pg_net.ttl to '{bad}'")) | ||
| msg = str(excinfo.value) | ||
| assert 'invalid value for parameter "pg_net.ttl"' in msg | ||
| assert "negative interval" in msg | ||
|
|
||
|
|
||
| def test_valid_ttl_formats_are_accepted(autocommit_sess): | ||
| """ | ||
| Any interval that postgres itself accepts must be accepted, | ||
| including compound values (issue #269) | ||
| """ | ||
| try: | ||
| for good in [ | ||
| "1 minute 10 seconds", | ||
| "90 seconds", | ||
| "1.5 hours", | ||
| "00:01:10", | ||
| "1 day 2 hours 3 minutes", | ||
| "500 milliseconds", | ||
| "0", | ||
| ]: | ||
| autocommit_sess.execute(text(f"alter system set pg_net.ttl to '{good}'")) | ||
| autocommit_sess.execute(text("select pg_reload_conf()")) | ||
| finally: | ||
| reset_ttl(autocommit_sess) | ||
|
|
||
|
|
||
| def test_compound_ttl_is_honored_by_worker(sess, autocommit_sess): | ||
| """ | ||
| A compound interval like '1 second 500 milliseconds' must be | ||
| applied by the worker as-is, i.e. a response must be expired | ||
| once that time has passed and the worker wakes (issue #269) | ||
| """ | ||
| try: | ||
| autocommit_sess.execute( | ||
| text("alter system set pg_net.ttl to '1 second 500 milliseconds'")) | ||
| restart_worker(autocommit_sess) | ||
|
|
||
| request_id = http_request(sess, text( | ||
| "select net.http_get('http://localhost:8080/anything')")) | ||
|
|
||
| response = collect_response_sync(sess, request_id) | ||
| assert response is not None | ||
| assert response["status"] == "SUCCESS" | ||
|
|
||
| # Sleep past the ttl so the response is expired, then wake the worker | ||
| time.sleep(1.6) | ||
| wakeup_worker(sess) | ||
|
|
||
| wait_for_response_count(autocommit_sess, 0) | ||
|
|
||
| finally: | ||
| reset_ttl(autocommit_sess) | ||
| restart_worker(autocommit_sess) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import pytest | ||
| from sqlalchemy import text | ||
|
|
||
|
|
||
| def test_wake_does_not_leak_xact_callbacks(sess, autocommit_sess): | ||
| """ | ||
| net.wake() registers a transaction callback so the worker is woken at | ||
| commit time. RegisterXactCallback appends a new entry on every call and | ||
| never deduplicates, so it must be registered at most once per backend. | ||
| Otherwise every transaction that calls net.http_* leaks an entry in | ||
| TopMemoryContext for the lifetime of the connection. | ||
|
|
||
| Measure TopMemoryContext of this backend before and after thousands of | ||
| single-statement transactions that call net.wake(): it must stay flat. | ||
| """ | ||
| (version_num,) = autocommit_sess.execute( | ||
| text("show server_version_num")).fetchone() | ||
| if int(version_num) < 140000: | ||
| pytest.skip("pg_backend_memory_contexts requires PostgreSQL 14+") | ||
|
|
||
| def top_memory_context_used_bytes(): | ||
| return autocommit_sess.execute(text(""" | ||
| select used_bytes | ||
| from pg_backend_memory_contexts | ||
| where name = 'TopMemoryContext' | ||
| """)).scalar_one() | ||
|
|
||
| iterations = 5000 | ||
|
|
||
| # warm up so one-off allocations (first callback registration, catalog | ||
| # lookups, etc.) don't count towards the measurement | ||
| for _ in range(50): | ||
| autocommit_sess.execute(text("select net.wake()")) | ||
|
|
||
| before = top_memory_context_used_bytes() | ||
|
|
||
| # each execute is its own transaction under autocommit, so each one | ||
| # runs wake() and then the commit callback | ||
| for _ in range(iterations): | ||
| autocommit_sess.execute(text("select net.wake()")) | ||
|
|
||
| after = top_memory_context_used_bytes() | ||
| growth = after - before | ||
| print(f"TopMemoryContext growth over {iterations} net.wake() transactions: {growth} bytes") | ||
|
|
||
| # a leaked callback entry costs 32 bytes (24-byte struct in a 32-byte | ||
| # AllocSet chunk), so the buggy behavior grows by iterations * 32 bytes | ||
| # (160 KB for 5000 iterations). Allow for some unrelated noise. | ||
| assert growth < 32 * 1024, ( | ||
| f"TopMemoryContext grew by {growth} bytes over {iterations} " | ||
| f"transactions calling net.wake(), expected it to stay flat" | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we assert the value of the full error message, that will make it easy to see the complete error we expect users to see as well.