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

Set custom temporary directory used by FF and delete it #1093

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
dbaea23
Set TMPDIR in the environment to a unique directory for each browser
ndanner-wesleyancs Jun 10, 2024
6e10ced
Reorganized temporary directory management.
ndanner-wesleyancs Jun 11, 2024
5849b93
Undid accidental adding of local mod to install-firefox.sh.
ndanner-wesleyancs Jun 11, 2024
ae882fe
Moved temporary directory cleanup to
ndanner-wesleyancs Jun 13, 2024
7477aa6
Delete leftover temporary directory.
ndanner-wesleyancs Jun 26, 2024
2e09752
Moved temporary directory deletion to finally block in
ndanner-wesleyancs Jul 2, 2024
bc0d6ae
Reformatted with `black` as per `CONTRIBUTING.md`.
ndanner-wesleyancs Jul 3, 2024
f98285c
Set TMPDIR in the environment to a unique directory for each browser
ndanner-wesleyancs Jun 10, 2024
9f033e2
Reorganized temporary directory management.
ndanner-wesleyancs Jun 11, 2024
492f3e0
Undid accidental adding of local mod to install-firefox.sh.
ndanner-wesleyancs Jun 11, 2024
c484cad
Moved temporary directory cleanup to
ndanner-wesleyancs Jun 13, 2024
3defbf8
Delete leftover temporary directory.
ndanner-wesleyancs Jun 26, 2024
2bcfca7
Moved temporary directory deletion to finally block in
ndanner-wesleyancs Jul 2, 2024
de1da0d
Reformatted with `black` as per `CONTRIBUTING.md`.
ndanner-wesleyancs Jul 3, 2024
d3c2e2d
Fixed mypy errors for everything but the instantiation of
ndanner-wesleyancs Jul 23, 2024
d643bb2
Merge branch 'delete_tmpdir' of github.com:ndanner-wesleyancs/OpenWPM…
ndanner-wesleyancs Jul 23, 2024
b002c5a
Merge branch 'master' into delete_tmpdir
vringar Aug 6, 2024
d87db54
Merge branch 'delete_tmpdir' of github.com:ndanner-wesleyancs/OpenWPM…
ndanner-wesleyancs Aug 23, 2024
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
51 changes: 48 additions & 3 deletions openwpm/browser_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ def launch_browser_manager(self) -> bool:

crash_recovery = True

# Create a unique temporary directory that we can delete
# when we shut down. Note that this doesn't force anything to
# use `tmpdir`, it just makes it available.
if self.browser_params.tmpdir is not None:
self.logger.debug(
"BROWSER %i: leftover temp directory %s? Deleting it."
% (self.browser_params.browser_id, self.browser_params.tmpdir)
)
try:
shutil.rmtree(self.browser_params.tmpdir)
except Exception as e:
self.logger.debug(
"BROWSER %i: error deleting %s: %s."
% (
self.browser_params.browser_id,
self.browser_params.tmpdir,
str(e),
)
)
self.browser_params.tmpdir = Path(
tempfile.mkdtemp(prefix="openwpm_", dir=os.getenv("TMPDIR", default="/tmp"))
)
self.logger.debug(
"BROWSER %i: Using temp dir %s"
% (self.browser_params.browser_id, self.browser_params.tmpdir)
)

self.logger.info("BROWSER %i: Launching browser..." % self.browser_id)
self.is_fresh = not crash_recovery

Expand Down Expand Up @@ -332,14 +359,32 @@ def close_browser_manager(self, force: bool = False) -> None:
)
return

self.logger.debug(
"BROWSER %i: Browser manager closed successfully." % self.browser_id
)
self.logger.debug(
"BROWSER %i: Browser manager closed successfully." % self.browser_id
)
shutdown_complete = True
finally:
if not shutdown_complete:
self.kill_browser_manager()

# Delete the temporary directory used by geckodriver.
try:
self.logger.debug(
"BROWSER %i: deleting temp dir %s"
% (self.browser_params.browser_id, self.browser_params.tmpdir)
)
shutil.rmtree(self.browser_params.tmpdir)
self.browser_params.tmpdir = None
except Exception as e:
self.logger.warn(
"BROWSER %i: failed to delete temp dir %s: %s"
% (
self.browser_params.browser_id,
self.browser_params.tmpdir,
str(e),
)
)

def execute_command_sequence(
self,
# Quoting to break cyclic import, see https://stackoverflow.com/a/39757388
Expand Down
13 changes: 13 additions & 0 deletions openwpm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class BrowserParams(DataClassJsonMixin):
default=Path(tempfile.gettempdir()),
metadata=DCJConfig(encoder=path_to_str, decoder=str_to_path),
)

"""
The tmp_profile_dir defaults to the OS's temporary file folder (typically /tmp) and is where the generated
browser profiles and residual files are stored.
Expand Down Expand Up @@ -140,6 +141,18 @@ class BrowserParams(DataClassJsonMixin):

"""

tmpdir: Optional[Path] = field(
default=None,
metadata=DCJConfig(encoder=path_to_str, decoder=str_to_path),
)
"""
The temporary directory used by `geckodriver`. This is configured in
`BrowserManager.run` and then deleted when the browser is finished. We do
this because it seems that `geckodriver` doesn't clean up its temporary
files (in particular, a copy of the extension XPI file), so we need to do
so ourselves.
"""

recovery_tar: Optional[Path] = None
donottrack: bool = False
tracking_protection: bool = False
Expand Down
9 changes: 9 additions & 0 deletions openwpm/deploy_browsers/deploy_firefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ def deploy_firefox(
# Launch the webdriver
status_queue.put(("STATUS", "Launch Attempted", None))

# Use browser_params.tmpdir as the temporary directory. This is so that
# geckodriver makes its copy of the extension XPI file in tmpdir, so
# we can delete it later and not have it left behind. I make a shallow
# copy of `os.environ` because I'm a little nervous about modifying the
# OpenWPM process' environment.
env = os.environ.copy()
env["TMPDIR"] = str(browser_params.tmpdir)

fo.binary_location = firefox_binary_path
geckodriver_path = subprocess.check_output(
"which geckodriver", encoding="utf-8", shell=True
Expand All @@ -150,6 +158,7 @@ def deploy_firefox(
service=Service(
executable_path=geckodriver_path,
log_output=open(webdriver_interceptor.fifo, "w"),
env=env,
),
)

Expand Down
Loading