Skip to content
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

SNOW-799408: Close temp file handle before removing temp cache file #1582

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne

- v3.0.5(TBD)
- Added the ability to read Snowflake's central configuration file.
- Improved OCSP response caching to remove tmp cache files on Windows.

- v3.0.4(May 23,2023)
- Fixed a bug in which `cursor.execute()` could modify the argument statement_params dictionary object when executing a multistatement query.
Expand Down
11 changes: 10 additions & 1 deletion src/snowflake/connector/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,16 @@ def _save(self, load_first: bool = True) -> bool:
prefix=fname,
dir=_dir,
)
with open(tmp_file, "wb") as w_file:
# tmp_file is an opened OS level handle, which means we need to close it manually.
# https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp
# ideally we shall just use the tmp_file fd to write,
# however, using os.write(tmp_file, bytes) causes seg fault during garbage collection when exiting
# python program.
# thus we fall back to the approach using the normal open() method to open a file and write.
os.close(tmp_file)
with open(tmp_file_path, "wb") as w_file:
sfc-gh-aling marked this conversation as resolved.
Show resolved Hide resolved
# note: during garbage collection when exiting python program, open will raise not defined
sfc-gh-aling marked this conversation as resolved.
Show resolved Hide resolved
# error as it has been released, we depend on this behavior to exit the program gracefully
pickle.dump(self, w_file)
# We write to a tmp file and then move it to have atomic write
os.replace(tmp_file_path, self.file_path)
Expand Down
18 changes: 17 additions & 1 deletion test/extras/run.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
#

import os
import pathlib
import subprocess
import sys

import snowflake.connector.ocsp_snowflake

# This script run every Python file in this directory other than this
# one in a subprocess and checks their exit codes

Expand All @@ -23,3 +25,17 @@
]
)
sub_process.check_returncode()
ocsp_cache_dir_path = pathlib.Path(
snowflake.connector.ocsp_snowflake.OCSP_RESPONSE_VALIDATION_CACHE.file_path
).parent
cache_files = set(os.listdir(ocsp_cache_dir_path))
# This is to test SNOW-79940, making sure tmp files are removed
# Windows does not have ocsp_response_validation_cache.lock
sfc-gh-aling marked this conversation as resolved.
Show resolved Hide resolved
assert cache_files == {
"ocsp_response_validation_cache.lock",
"ocsp_response_validation_cache",
"ocsp_response_cache.json",
} or cache_files == {
"ocsp_response_validation_cache",
"ocsp_response_cache.json",
}