# Buggy code
def stop(self):
    """Stop a managed service"""
    if not self.managed:
        raise RuntimeError("Cannot stop unmanaged service %s" % self)
    if self.spawner:
        self.spawner.stop_polling()
        return self.spawner.stop()


# Traceback (sqlalchemy.exc.IntegrityError)
singleuser.cmd jupyterhub-singleuser
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/jupyterhub/app.py", line 1623, in launch_instance_async
yield self.initialize(argv)
File "/usr/lib/python3.5/types.py", line 179, in throw
return self.__wrapped.throw(tp, *rest)
File "/usr/local/lib/python3.5/dist-packages/jupyterhub/app.py", line 1385, in initialize
yield self.init_spawners()
File "/usr/local/lib/python3.5/dist-packages/jupyterhub/app.py", line 1240, in init_spawners
db.commit()
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/session.py", line 906, in commit
self.transaction.commit()
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/session.py", line 461, in commit
self._prepare_impl()
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/session.py", line 441, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/session.py", line 2171, in flush
self._flush(objects)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/session.py", line 2291, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 187, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/session.py", line 2255, in _flush
flush_context.execute()
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/unitofwork.py", line 389, in execute
rec.execute(self)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/unitofwork.py", line 577, in execute
uow
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/persistence.py", line 258, in delete_obj
cached_connections, mapper, table, delete)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/persistence.py", line 931, in _emit_delete_statements
c = connection.execute(statement, del_objects)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 945, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
exc_info
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) update or delete on table "servers" violates foreign key constraint "spawners_server_id_fkey" on table "spawners"
DETAIL:  Key (id)=(24433) is still referenced from table "spawners".
[SQL: 'DELETE FROM servers WHERE servers.id = %(id)s'] [parameters: {'id': 24433}]

# Fixed code
def stop(self):
    """Stop a managed service"""
    if not self.managed:
        raise RuntimeError("Cannot stop unmanaged service %s" % self)
    if self.spawner:
        if self.orm.server:
            self.db.delete(self.orm.server)
            self.db.commit()
        self.spawner.stop_polling()
        return self.spawner.stop()