# Buggy code
def do_create_user(
    email,
    password,
    realm,
    full_name,
    short_name,
    active=True,
    is_realm_admin=False,
    bot_type=None,
    bot_owner=None,
    tos_version=None,
    timezone="",
    avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,
    default_sending_stream=None,
    default_events_register_stream=None,
    default_all_public_streams=None,
    prereg_user=None,
    newsletter_data=None,
    default_stream_groups=[],
):
    # type: (Text, Optional[Text], Realm, Text, Text, bool, bool, Optional[int], Optional[UserProfile], Optional[Text], Text, Text, Optional[Stream], Optional[Stream], bool, Optional[PreregistrationUser], Optional[Dict[str, str]], List[DefaultStreamGroup]) -> UserProfile

    user_profile = create_user(
        email=email,
        password=password,
        realm=realm,
        full_name=full_name,
        short_name=short_name,
        active=active,
        is_realm_admin=is_realm_admin,
        bot_type=bot_type,
        bot_owner=bot_owner,
        tos_version=tos_version,
        timezone=timezone,
        avatar_source=avatar_source,
        default_sending_stream=default_sending_stream,
        default_events_register_stream=default_events_register_stream,
        default_all_public_streams=default_all_public_streams,
    )

    event_time = user_profile.date_joined
    RealmAuditLog.objects.create(
        realm=user_profile.realm,
        modified_user=user_profile,
        event_type="user_created",
        event_time=event_time,
    )
    do_increment_logging_stat(
        user_profile.realm,
        COUNT_STATS["active_users_log:is_bot:day"],
        user_profile.is_bot,
        event_time,
    )

    notify_created_user(user_profile)
    if bot_type:
        notify_created_bot(user_profile)
    else:
        process_new_human_user(
            user_profile,
            prereg_user=prereg_user,
            newsletter_data=newsletter_data,
            default_stream_groups=default_stream_groups,
        )
    return user_profile


# Traceback (django.core.exceptions.ValidationError)
Running zerver.tests.test_home.HomeTest.test_people
2017-10-27 22:23:35.802 ERR  [] Error queueing internal message by welcome-bot@zulip.com: 'defunct-0@zulip.com' is no longer using Zulip.
Traceback (most recent call last):
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1760, in check_message
forwarder_user_profile, sender)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1544, in recipient_for_user_profiles
recipient_profile_ids = validate_recipient_user_profiles(user_profiles, sender)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1517, in validate_recipient_user_profiles
raise ValidationError(_("'%s' is no longer using Zulip.") % (user_profile.email,))
django.core.exceptions.ValidationError: ["'defunct-0@zulip.com' is no longer using Zulip."]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1811, in _internal_prep_message
content, realm=realm)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1763, in check_message
raise JsonableError(e.messages[0])
zerver.lib.exceptions.JsonableError: 'defunct-0@zulip.com' is no longer using Zulip.
2017-10-27 22:23:35.828 ERR  [] Error queueing internal message by welcome-bot@zulip.com: 'defunct-1@zulip.com' is no longer using Zulip.
Traceback (most recent call last):
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1760, in check_message
forwarder_user_profile, sender)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1544, in recipient_for_user_profiles
recipient_profile_ids = validate_recipient_user_profiles(user_profiles, sender)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1517, in validate_recipient_user_profiles
raise ValidationError(_("'%s' is no longer using Zulip.") % (user_profile.email,))
django.core.exceptions.ValidationError: ["'defunct-1@zulip.com' is no longer using Zulip."]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1811, in _internal_prep_message
content, realm=realm)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1763, in check_message
raise JsonableError(e.messages[0])
zerver.lib.exceptions.JsonableError: 'defunct-1@zulip.com' is no longer using Zulip.
2017-10-27 22:23:35.852 ERR  [] Error queueing internal message by welcome-bot@zulip.com: 'defunct-2@zulip.com' is no longer using Zulip.
Traceback (most recent call last):
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1760, in check_message
forwarder_user_profile, sender)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1544, in recipient_for_user_profiles
recipient_profile_ids = validate_recipient_user_profiles(user_profiles, sender)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1517, in validate_recipient_user_profiles
raise ValidationError(_("'%s' is no longer using Zulip.") % (user_profile.email,))
django.core.exceptions.ValidationError: ["'defunct-2@zulip.com' is no longer using Zulip."]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1811, in _internal_prep_message
content, realm=realm)
File "/home/tabbott/zulip/zerver/lib/actions.py", line 1763, in check_message
raise JsonableError(e.messages[0])
zerver.lib.exceptions.JsonableError: 'defunct-2@zulip.com' is no longer using Zulip.

# Fixed code
def do_create_user(
    email,
    password,
    realm,
    full_name,
    short_name,
    is_realm_admin=False,
    bot_type=None,
    bot_owner=None,
    tos_version=None,
    timezone="",
    avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,
    default_sending_stream=None,
    default_events_register_stream=None,
    default_all_public_streams=None,
    prereg_user=None,
    newsletter_data=None,
    default_stream_groups=[],
):
    # type: (Text, Optional[Text], Realm, Text, Text, bool, Optional[int], Optional[UserProfile], Optional[Text], Text, Text, Optional[Stream], Optional[Stream], bool, Optional[PreregistrationUser], Optional[Dict[str, str]], List[DefaultStreamGroup]) -> UserProfile

    user_profile = create_user(
        email=email,
        password=password,
        realm=realm,
        full_name=full_name,
        short_name=short_name,
        is_realm_admin=is_realm_admin,
        bot_type=bot_type,
        bot_owner=bot_owner,
        tos_version=tos_version,
        timezone=timezone,
        avatar_source=avatar_source,
        default_sending_stream=default_sending_stream,
        default_events_register_stream=default_events_register_stream,
        default_all_public_streams=default_all_public_streams,
    )

    event_time = user_profile.date_joined
    RealmAuditLog.objects.create(
        realm=user_profile.realm,
        modified_user=user_profile,
        event_type="user_created",
        event_time=event_time,
    )
    do_increment_logging_stat(
        user_profile.realm,
        COUNT_STATS["active_users_log:is_bot:day"],
        user_profile.is_bot,
        event_time,
    )

    notify_created_user(user_profile)
    if bot_type:
        notify_created_bot(user_profile)
    else:
        process_new_human_user(
            user_profile,
            prereg_user=prereg_user,
            newsletter_data=newsletter_data,
            default_stream_groups=default_stream_groups,
        )
    return user_profile