# Buggy code
def compute_last_index(self, c):
    """Scan the entire leo outline to compute ni.last_index."""
    trace = False and not g.unitTesting
    verbose = False  # Report only if lastIndex was changed.
    if trace:
        t1 = time.time()
    ni = self
    old_lastIndex = self.lastIndex
    self.lastIndex = 0
    for v in c.all_unique_nodes():
        gnx = v.fileIndex
        if gnx:
            id_, t, n = self.scanGnx(gnx)
            if t == ni.timeString and n is not None:
                try:
                    n = int(n)
                    self.lastIndex = max(self.lastIndex, n)
                except Exception:
                    g.es_exception()
                    self.lastIndex += 1
    if trace:
        changed = self.lastIndex > old_lastIndex
        t2 = time.time()
        if verbose:
            g.trace(
                "========== time %4.2f changed: %5s lastIndex: old: %s new: %s"
                % (t2 - t1, changed, old_lastIndex, self.lastIndex)
            )
        elif changed:
            g.trace(
                "========== time %4.2f lastIndex: old: %s new: %s"
                % (t2 - t1, old_lastIndex, self.lastIndex)
            )


# 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 compute_last_index(self, c):
    """Scan the entire leo outline to compute ni.last_index."""
    trace = False and not g.unitTesting
    verbose = True  # Report only if lastIndex was changed.
    if trace:
        t1 = time.time()
    ni = self
    old_lastIndex = self.lastIndex
    # Partial, experimental, fix for #658.
    # Do not change self.lastIndex here!
    # self.lastIndex = 0
    for v in c.all_unique_nodes():
        gnx = v.fileIndex
        if trace and verbose:
            g.trace(gnx)
        if gnx:
            id_, t, n = self.scanGnx(gnx)
            if t == ni.timeString and n is not None:
                try:
                    n = int(n)
                    self.lastIndex = max(self.lastIndex, n)
                    if trace and verbose:
                        g.trace(n, gnx)
                except Exception:
                    g.es_exception()
                    self.lastIndex += 1
    if trace:
        changed = self.lastIndex > old_lastIndex
        t2 = time.time()
        if verbose:
            g.trace(
                "========== time %4.2f changed: %5s lastIndex: old: %s new: %s"
                % (t2 - t1, changed, old_lastIndex, self.lastIndex)
            )
        elif changed:
            g.trace(
                "========== time %4.2f lastIndex: old: %s new: %s"
                % (t2 - t1, old_lastIndex, self.lastIndex)
            )