# Buggy code
def openLeoFile(self, fileName):
    """Open a .leo file, or create a new Leo frame if no fileName is given."""
    g = self.g
    g.app.silentMode = self.silentMode
    useLog = False
    if self.isOpen():
        fileName = self.completeFileName(fileName)
        c = self.createFrame(fileName)
        g.app.nodeIndices.compute_last_index(c)
        # New in Leo 5.1. An alternate fix for bug #130.
        # When using a bridge Leo might open a file, modify it,
        # close it, reopen it and change it all within one second.
        # In that case, this code must properly compute the next
        # available gnx by scanning the entire outline.
        if useLog:
            g.app.gui.log = log = c.frame.log
            log.isNull = False
            log.enabled = True
        return c
    return None


# Traceback (sqlite3.ProgrammingError)
2018-01-25 14:32:51 /pri/git/leo_bug_demos/2018-01-25-leoBridge-Closed-sqlite3
$ python3 opOnClosedSqlite3.py
Traceback (most recent call last):
File "opOnClosedSqlite3.py", line 23, in <module>
main()
File "opOnClosedSqlite3.py", line 20, in main
cmdrX.close()
File "/pri/git/leo-editor/leo/commands/commanderFileCommands.py", line 51, in close
g.app.closeLeoWindow(self.frame, new_c=new_c)
File "/pri/git/leo-editor/leo/core/leoApp.py", line 1277, in closeLeoWindow
g.app.forgetOpenFile(fn=c.fileName(), force=True)
File "/pri/git/leo-editor/leo/core/leoApp.py", line 1398, in forgetOpenFile
aList = d.get(tag) or []
File "/pri/git/leo-editor/leo/core/leoCache.py", line 934, in get
if not self.has_key(key):return default
File "/pri/git/leo-editor/leo/core/leoCache.py", line 945, in has_key
for row in self.conn.execute(sql, (key,)):
sqlite3.ProgrammingError: Cannot operate on a closed database.
2018-01-25 14:39:08 /pri/git/leo_bug_demos/2018-01-25-leoBridge-Closed-sqlite3
$ python3 opOnClosedSqlite3.py

Internal Leo error in check_gnx
getNewIndex: gnx clash bob.20180125143918.2: v: <VNode 7f99af4160f0 newHeadline> v2: <VNode 7f99af40f710 NewHeadline>
Called from <module>, main, insertAfter, __init__, new_vnode_helper, getNewIndex
Please report this error to Leo's developers
Traceback (most recent call last):
File "opOnClosedSqlite3.py", line 23, in <module>
main()
File "opOnClosedSqlite3.py", line 13, in main
cmdrX = bridge.openLeoFile('bugdemo{0:02d}.leo'.format(fidx))
File "/pri/git/leo-editor/leo/core/leoBridge.py", line 259, in openLeoFile
c = self.createFrame(fileName)
File "/pri/git/leo-editor/leo/core/leoBridge.py", line 293, in createFrame
c = g.openWithFileName(fileName)
File "/pri/git/leo-editor/leo/core/leoGlobals.py", line 3318, in openWithFileName
return g.app.loadManager.loadLocalFile(fileName, gui, old_c)
File "/pri/git/leo-editor/leo/core/leoApp.py", line 3058, in loadLocalFile
c = lm.openFileByName(fn, gui, old_c, previousSettings)
File "/pri/git/leo-editor/leo/core/leoApp.py", line 3099, in openFileByName
ok = lm.readOpenedLeoFile(c, fn, readAtFileNodesFlag, theFile)
File "/pri/git/leo-editor/leo/core/leoApp.py", line 3274, in readOpenedLeoFile
readAtFileNodesFlag=readAtFileNodesFlag)
File "/pri/git/leo-editor/leo/core/leoFileCommands.py", line 843, in openLeoFile
silent=silent,
File "/pri/git/leo-editor/leo/core/leoFileCommands.py", line 658, in getLeoFile
g.app.checkForOpenFile(c, fileName)
File "/pri/git/leo-editor/leo/core/leoApp.py", line 1376, in checkForOpenFile
aList = g.app.db.get(tag) or []
File "/pri/git/leo-editor/leo/core/leoCache.py", line 934, in get
if not self.has_key(key):return default
File "/pri/git/leo-editor/leo/core/leoCache.py", line 945, in has_key
for row in self.conn.execute(sql, (key,)):
sqlite3.ProgrammingError: Cannot operate on a closed database.
2018-01-25 14:39:18 /pri/git/leo_bug_demos/2018-01-25-leoBridge-Closed-sqlite3

# Fixed code
def openLeoFile(self, fileName):
    """Open a .leo file, or create a new Leo frame if no fileName is given."""
    g = self.g
    g.app.silentMode = self.silentMode
    useLog = False
    if self.isOpen():
        self.reopen_cachers()
        fileName = self.completeFileName(fileName)
        c = self.createFrame(fileName)
        g.app.nodeIndices.compute_last_index(c)
        # New in Leo 5.1. An alternate fix for bug #130.
        # When using a bridge Leo might open a file, modify it,
        # close it, reopen it and change it all within one second.
        # In that case, this code must properly compute the next
        # available gnx by scanning the entire outline.
        if useLog:
            g.app.gui.log = log = c.frame.log
            log.isNull = False
            log.enabled = True
        return c
    return None