diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
index d89a752..9022adb 100644
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -2540,6 +2540,11 @@ class InteractiveShell(SingletonConfigurable, Magic):
         code that has the appropriate information, rather than trying to
         clutter 
         """
+        # Close the history session (this stores the end time and line count)
+        # this must be *before* the tempfile cleanup, in case of temporary
+        # history db
+        self.history_manager.end_session()
+        
         # Cleanup all tempfiles left around
         for tfile in self.tempfiles:
             try:
@@ -2547,9 +2552,6 @@ class InteractiveShell(SingletonConfigurable, Magic):
             except OSError:
                 pass
         
-        # Close the history session (this stores the end time and line count)
-        self.history_manager.end_session()
-        
         # Clear all user namespaces to release all references cleanly.
         self.reset(new_session=False)
 
diff --git a/IPython/testing/globalipapp.py b/IPython/testing/globalipapp.py
index cee6f2b..4315da3 100644
--- a/IPython/testing/globalipapp.py
+++ b/IPython/testing/globalipapp.py
@@ -194,6 +194,9 @@ def start_ipython():
 
     # A few more tweaks needed for playing nicely with doctests...
     
+    # remove history file
+    shell.tempfiles.append(config.HistoryManager.hist_file)
+    
     # These traps are normally only active for interactive use, set them
     # permanently since we'll be mocking interactive sessions.
     shell.builtin_trap.activate()
diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py
index 60f98d8..e76e85d 100644
--- a/IPython/testing/tools.py
+++ b/IPython/testing/tools.py
@@ -171,7 +171,7 @@ def default_config():
     config.TerminalInteractiveShell.colors = 'NoColor'
     config.TerminalTerminalInteractiveShell.term_title = False,
     config.TerminalInteractiveShell.autocall = 0
-    config.HistoryManager.hist_file = os.path.join(tempfile.mkdtemp(), u'test_hist.sqlite')
+    config.HistoryManager.hist_file = tempfile.mktemp(u'test_hist.sqlite')
     config.HistoryManager.db_cache_size = 10000
     return config