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

DM-11771: Close files that have been left hanging #66

Merged
merged 6 commits into from
Sep 2, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions python/lsst/obs/base/cameraMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ def search(filename, description):
localFileObj = storage.getLocalFile(path)
self.log.info("Loading %s registry from %s", description, localFileObj.name)
registry = dafPersist.Registry.create(localFileObj.name)
localFileObj.close()
elif not registry and posixIfNoSql:
try:
self.log.info("Loading Posix %s registry from %s", description, storage.root)
Expand Down
39 changes: 34 additions & 5 deletions tests/testCameraMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

from builtins import range
import collections
import gc
import os
import sqlite3
import unittest
import tempfile

Expand Down Expand Up @@ -390,15 +392,39 @@ def testPackageName(self):

class ParentRegistryTestCase(unittest.TestCase):

@staticmethod
def _createRegistry(path):
cmd = """CREATE TABLE x(
id INT,
visit INT,
filter TEXT,
snap INT,
raft TEXT,
sensor TEXT,
channel TEXT,
taiObs TEXT,
expTime REAL
);
"""
conn = sqlite3.connect(path)
conn.cursor().execute(cmd)
conn.commit()
conn.close()

def setUp(self):
self.ROOT = tempfile.mkdtemp(dir=ROOT, prefix="ParentRegistryTestCase-")
self.repoARoot = os.path.join(self.ROOT, 'a')
args = dafPersist.RepositoryArgs(root=self.repoARoot, mapper=MinMapper1)
butler = dafPersist.Butler(outputs=args)
with open(os.path.join(self.repoARoot, 'registry.sqlite3'), 'w') as f:
f.write('123')
self._createRegistry(os.path.join(self.repoARoot, 'registry.sqlite3'))
del butler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be needed, but keeping it means that the linter won't complain about butler only being used once.


def tearDown(self):
# the butler sql registry closes its database connection in __del__. To trigger __del__ we explicitly
# collect the garbage here. If we find having or closing the open database connection is a problem in
# production code, we may need to add api to butler to explicity release database connections (and
# maybe other things like in-memory cached objects).
gc.collect()
if os.path.exists(self.ROOT):
shutil.rmtree(self.ROOT)

Expand All @@ -413,16 +439,19 @@ def test(self):
registryA = butler._repos.inputs()[0].repo._mapper.registry
registryB = butler._repos.outputs()[0].repo._mapper.registry
self.assertEqual(id(registryA), id(registryB))
del registryA
del registryB
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am guessing that these three del calls are not needed because the assignment later will overwrite them.

del butler

with open(os.path.join(repoBRoot, 'registry.sqlite3'), 'w') as f:
f.write('123')
self._createRegistry(os.path.join(repoBRoot, 'registry.sqlite3'))
butler = dafPersist.Butler(inputs=self.repoARoot, outputs=repoBRoot)
# see above; don't copy this way of getting the registry.
registryA = butler._repos.inputs()[0].repo._mapper.registry
registryB = butler._repos.outputs()[0].repo._mapper.registry
self.assertNotEqual(id(registryA), id(registryB))

del registryA
del registryB
del butler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these make a difference now? The variables will disappear at end of scope and the gc.collect() will clean up.


class MissingPolicyKeyTestCase(unittest.TestCase):

Expand Down