I think we've found a bug:
Here's what I suspect is the problem code in getUserGroups():
$groups= $_SESSION["modx.user.{$id}.userGroups"];
And here's the relevant code in joinGroup():
unset($_SESSION["modx.user.{$this->get('id')}.userGroupNames"]);
Notice that they're referencing two different array members.
If you add this to your code after the joinGroup(), it may work:
$userId = $modx->user->get('id');
unset($_SESSION["modx.user.{$userId}.userGroups"]);