    def preexec():
        """Set uid/gid of current process

        Executed after fork but before exec by python.

        Also try to chdir to the user's home directory.
        """
        os.setgid(gid)
        try:
            os.setgroups(gids)
        except Exception as e:
            print(f'Failed to set groups {e}', file=sys.stderr)
        os.setuid(uid)

        # start in the user's home dir
        if chdir:
            _try_setcwd(home)
Set uid/gid of current process

Executed after fork but before exec by python.

Also try to chdir to the user's home directory.
jupyterhub/spawner.py