This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
checks for generators in database functions #11564
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
695f07f
checks for generators in database functions
richvdh 59d1894
fix add_users_to_send_full_presence_to
richvdh 993edae
pacify mypy
richvdh 4fca9f7
add more docstring on `new_transaction`
richvdh 601a6d8
Actually log where the error is
richvdh 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 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 @@ | ||
Add some safety checks that storage functions are used correctly. |
This file contains 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 |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import inspect | ||
import logging | ||
import time | ||
import types | ||
from collections import defaultdict | ||
from sys import intern | ||
from time import monotonic as monotonic_time | ||
|
@@ -526,6 +528,12 @@ def new_transaction( | |
the function will correctly handle being aborted and retried half way | ||
through its execution. | ||
|
||
Similarly, the arguments to `func` (`args`, `kwargs`) should not be generators, | ||
since they could be evaluated multiple times (which would produce an empty | ||
result on the second or subsequent evaluation). Likewise, the closure of `func` | ||
must not reference any generators. This method attempts to detect such usage | ||
and will log an error. | ||
|
||
Args: | ||
conn | ||
desc | ||
|
@@ -536,6 +544,39 @@ def new_transaction( | |
**kwargs | ||
""" | ||
|
||
# Robustness check: ensure that none of the arguments are generators, since that | ||
# will fail if we have to repeat the transaction. | ||
# For now, we just log an error, and hope that it works on the first attempt. | ||
# TODO: raise an exception. | ||
for i, arg in enumerate(args): | ||
if inspect.isgenerator(arg): | ||
logger.error( | ||
"Programming error: generator passed to new_transaction as " | ||
"argument %i to function %s", | ||
i, | ||
func, | ||
) | ||
for name, val in kwargs.items(): | ||
if inspect.isgenerator(val): | ||
logger.error( | ||
"Programming error: generator passed to new_transaction as " | ||
"argument %s to function %s", | ||
name, | ||
func, | ||
) | ||
# also check variables referenced in func's closure | ||
if inspect.isfunction(func): | ||
f = cast(types.FunctionType, func) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mypy doesn't understand that |
||
if f.__closure__: | ||
for i, cell in enumerate(f.__closure__): | ||
if inspect.isgenerator(cell.cell_contents): | ||
logger.error( | ||
"Programming error: function %s references generator %s " | ||
"via its closure", | ||
f, | ||
f.__code__.co_freevars[i], | ||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
start = monotonic_time() | ||
txn_id = self._TXN_ID | ||
|
||
|
@@ -1226,9 +1267,9 @@ async def simple_upsert_many( | |
self, | ||
table: str, | ||
key_names: Collection[str], | ||
key_values: Collection[Iterable[Any]], | ||
key_values: Collection[Collection[Any]], | ||
value_names: Collection[str], | ||
value_values: Iterable[Iterable[Any]], | ||
value_values: Collection[Collection[Any]], | ||
Comment on lines
+1270
to
+1272
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, because |
||
desc: str, | ||
) -> None: | ||
""" | ||
|
@@ -1920,7 +1961,7 @@ async def simple_delete_many( | |
self, | ||
table: str, | ||
column: str, | ||
iterable: Iterable[Any], | ||
iterable: Collection[Any], | ||
keyvalues: Dict[str, Any], | ||
desc: str, | ||
) -> int: | ||
|
@@ -1931,7 +1972,8 @@ async def simple_delete_many( | |
Args: | ||
table: string giving the table name | ||
column: column name to test for inclusion against `iterable` | ||
iterable: list | ||
iterable: list of values to match against `column`. NB cannot be a generator | ||
as it may be evaluated multiple times. | ||
keyvalues: dict of column names and values to select the rows with | ||
desc: description of the transaction, for logging and metrics | ||
|
||
|
This file contains 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
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.
Should we update the docstring of this method to specifically call out not using generators?
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.
fair, will do.