Phabricator errors while importing libphutil using conduit calls?

Observed Behavior:
Trying to import the libphutil repository takes a long time and generates error messages like the following:

Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000] [2019-05-21 05:38:55] EXCEPTION: (PhutilProxyException) Error while executing Task ID 234. {>} (AphrontDuplicateKeyQueryException) #1062: Duplicate entry 'RtFNsLvP.889' for key 'key_identity' at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:335]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000] arcanist(head=stable, ref.stable=26452002a256), phabricator(head=stable, ref.stable=3337b84073f9), phutil(head=stable, ref.stable=1185300a23d9)
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #0 <#2> AphrontBaseMySQLDatabaseConnection::throwCommonException(integer, string) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:369]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #1 <#2> AphrontBaseMySQLDatabaseConnection::throwQueryCodeException(integer, string) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:310]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #2 <#2> AphrontBaseMySQLDatabaseConnection::throwQueryException(mysqli) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:206]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #3 <#2> AphrontBaseMySQLDatabaseConnection::executeQuery(PhutilQueryString) called at [<phutil>/src/xsprintf/queryfx.php:8]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #4 <#2> queryfx(AphrontMySQLiDatabaseConnection, string, PhutilQueryString, PhabricatorRepositoryIdentity, array, array) called at [<phutil>/src/aphront/storage/connection/AphrontDatabaseConnection.php:58]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #5 <#2> AphrontDatabaseConnection::query(string, PhutilQueryString, PhabricatorRepositoryIdentity, array, array) called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:1107]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #6 <#2> LiskDAO::insertRecordIntoDatabase(string) called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:939]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #7 <#2> LiskDAO::insert() called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:908]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #8 <#2> LiskDAO::save() called at [<phabricator>/src/applications/repository/storage/PhabricatorRepositoryIdentity.php:95]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #9 <#2> PhabricatorRepositoryIdentity::save() called at [<phabricator>/src/applications/repository/worker/commitmessageparser/PhabricatorRepositoryCommitMessageParserWorker.php:80]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #10 <#2> PhabricatorRepositoryCommitMessageParserWorker::updateCommitData(DiffusionCommitRef) called at [<phabricator>/src/applications/repository/worker/commitmessageparser/PhabricatorRepositoryCommitMessageParserWorker.php:42]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #11 <#2> PhabricatorRepositoryCommitMessageParserWorker::parseCommit(PhabricatorRepository, PhabricatorRepositoryCommit) called at [<phabricator>/src/applications/repository/worker/PhabricatorRepositoryCommitParserWorker.php:51]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #12 <#2> PhabricatorRepositoryCommitParserWorker::doWork() called at [<phabricator>/src/infrastructure/daemon/workers/PhabricatorWorker.php:124]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #13 <#2> PhabricatorWorker::executeTask() called at [<phabricator>/src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php:158]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #14 <#2> PhabricatorWorkerActiveTask::executeTask() called at [<phabricator>/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php:22]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #15 PhabricatorTaskmasterDaemon::run() called at [<phutil>/src/daemon/PhutilDaemon.php:219]
Daemon 22 STDE [Tue, 21 May 2019 05:38:55 +0000]   #16 PhutilDaemon::execute() called at [<phutil>/scripts/daemon/exec/exec_daemon.php:131]
Daemon 22 FAIL [Tue, 21 May 2019 05:38:55 +0000] Process exited with error 255.
Daemon 22 WAIT [Tue, 21 May 2019 05:38:55 +0000] Waiting 5 second(s) to restart process.
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000] [2019-05-21 05:38:58] EXCEPTION: (PhutilProxyException) Error while executing Task ID 355. {>} (AphrontDuplicateKeyQueryException) #1062: Duplicate entry 'ZNZUWwnz5oEB' for key 'key_identity' at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:335]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000] arcanist(head=stable, ref.stable=26452002a256), phabricator(head=stable, ref.stable=3337b84073f9), phutil(head=stable, ref.stable=1185300a23d9)
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #0 <#2> AphrontBaseMySQLDatabaseConnection::throwCommonException(integer, string) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:369]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #1 <#2> AphrontBaseMySQLDatabaseConnection::throwQueryCodeException(integer, string) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:310]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #2 <#2> AphrontBaseMySQLDatabaseConnection::throwQueryException(mysqli) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:206]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #3 <#2> AphrontBaseMySQLDatabaseConnection::executeQuery(PhutilQueryString) called at [<phutil>/src/xsprintf/queryfx.php:8]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #4 <#2> queryfx(AphrontMySQLiDatabaseConnection, string, PhutilQueryString, PhabricatorRepositoryIdentity, array, array) called at [<phutil>/src/aphront/storage/connection/AphrontDatabaseConnection.php:58]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #5 <#2> AphrontDatabaseConnection::query(string, PhutilQueryString, PhabricatorRepositoryIdentity, array, array) called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:1107]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #6 <#2> LiskDAO::insertRecordIntoDatabase(string) called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:939]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #7 <#2> LiskDAO::insert() called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:908]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #8 <#2> LiskDAO::save() called at [<phabricator>/src/applications/repository/storage/PhabricatorRepositoryIdentity.php:95]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #9 <#2> PhabricatorRepositoryIdentity::save() called at [<phabricator>/src/applications/repository/worker/commitmessageparser/PhabricatorRepositoryCommitMessageParserWorker.php:80]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #10 <#2> PhabricatorRepositoryCommitMessageParserWorker::updateCommitData(DiffusionCommitRef) called at [<phabricator>/src/applications/repository/worker/commitmessageparser/PhabricatorRepositoryCommitMessageParserWorker.php:42]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #11 <#2> PhabricatorRepositoryCommitMessageParserWorker::parseCommit(PhabricatorRepository, PhabricatorRepositoryCommit) called at [<phabricator>/src/applications/repository/worker/PhabricatorRepositoryCommitParserWorker.php:51]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #12 <#2> PhabricatorRepositoryCommitParserWorker::doWork() called at [<phabricator>/src/infrastructure/daemon/workers/PhabricatorWorker.php:124]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #13 <#2> PhabricatorWorker::executeTask() called at [<phabricator>/src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php:158]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #14 <#2> PhabricatorWorkerActiveTask::executeTask() called at [<phabricator>/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php:22]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #15 PhabricatorTaskmasterDaemon::run() called at [<phutil>/src/daemon/PhutilDaemon.php:219]
Daemon 21 STDE [Tue, 21 May 2019 05:38:58 +0000]   #16 PhutilDaemon::execute() called at [<phutil>/scripts/daemon/exec/exec_daemon.php:131]
Daemon 21 FAIL [Tue, 21 May 2019 05:38:58 +0000] Process exited with error 255.
Daemon 21 WAIT [Tue, 21 May 2019 05:38:58 +0000] Waiting 5 second(s) to restart process.
Daemon 22 INIT [Tue, 21 May 2019 05:39:00 +0000] Starting process.
Daemon 21 INIT [Tue, 21 May 2019 05:39:03 +0000] Starting process.
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000] [2019-05-21 05:39:18] EXCEPTION: (PhutilProxyException) Error while executing Task ID 958. {>} (AphrontDuplicateKeyQueryException) #1062: Duplicate entry 'y645YaER8ql.' for key 'key_identity' at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:335]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000] arcanist(head=stable, ref.stable=26452002a256), phabricator(head=stable, ref.stable=3337b84073f9), phutil(head=stable, ref.stable=1185300a23d9)
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #0 <#2> AphrontBaseMySQLDatabaseConnection::throwCommonException(integer, string) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:369]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #1 <#2> AphrontBaseMySQLDatabaseConnection::throwQueryCodeException(integer, string) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:310]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #2 <#2> AphrontBaseMySQLDatabaseConnection::throwQueryException(mysqli) called at [<phutil>/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php:206]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #3 <#2> AphrontBaseMySQLDatabaseConnection::executeQuery(PhutilQueryString) called at [<phutil>/src/xsprintf/queryfx.php:8]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #4 <#2> queryfx(AphrontMySQLiDatabaseConnection, string, PhutilQueryString, PhabricatorRepositoryIdentity, array, array) called at [<phutil>/src/aphront/storage/connection/AphrontDatabaseConnection.php:58]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #5 <#2> AphrontDatabaseConnection::query(string, PhutilQueryString, PhabricatorRepositoryIdentity, array, array) called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:1107]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #6 <#2> LiskDAO::insertRecordIntoDatabase(string) called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:939]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #7 <#2> LiskDAO::insert() called at [<phabricator>/src/infrastructure/storage/lisk/LiskDAO.php:908]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #8 <#2> LiskDAO::save() called at [<phabricator>/src/applications/repository/storage/PhabricatorRepositoryIdentity.php:95]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #9 <#2> PhabricatorRepositoryIdentity::save() called at [<phabricator>/src/applications/repository/worker/commitmessageparser/PhabricatorRepositoryCommitMessageParserWorker.php:80]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #10 <#2> PhabricatorRepositoryCommitMessageParserWorker::updateCommitData(DiffusionCommitRef) called at [<phabricator>/src/applications/repository/worker/commitmessageparser/PhabricatorRepositoryCommitMessageParserWorker.php:42]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #11 <#2> PhabricatorRepositoryCommitMessageParserWorker::parseCommit(PhabricatorRepository, PhabricatorRepositoryCommit) called at [<phabricator>/src/applications/repository/worker/PhabricatorRepositoryCommitParserWorker.php:51]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #12 <#2> PhabricatorRepositoryCommitParserWorker::doWork() called at [<phabricator>/src/infrastructure/daemon/workers/PhabricatorWorker.php:124]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #13 <#2> PhabricatorWorker::executeTask() called at [<phabricator>/src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php:158]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #14 <#2> PhabricatorWorkerActiveTask::executeTask() called at [<phabricator>/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php:22]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #15 PhabricatorTaskmasterDaemon::run() called at [<phutil>/src/daemon/PhutilDaemon.php:219]
Daemon 19 STDE [Tue, 21 May 2019 05:39:18 +0000]   #16 PhutilDaemon::execute() called at [<phutil>/scripts/daemon/exec/exec_daemon.php:131]
Daemon 19 FAIL [Tue, 21 May 2019 05:39:18 +0000] Process exited with error 255.
Daemon 19 WAIT [Tue, 21 May 2019 05:39:18 +0000] Waiting 5 second(s) to restart process.

Expected Behavior:
Phabricator should finish the import quickly (in fact importing the phabricator repo finished extremely quickly).

Phabricator Version:

phabricator
    3337b84073f992590e6241e612946b33a2d26e8a (Thu, May 16) 
arcanist
    26452002a2560eb340921a9798860041229163d9 (Thu, May 16) 
phutil
    1185300a23d9b0a611ecaf8c99e90c92097f5c67 (Thu, May 16) 
php
    7.2.17-0ubuntu0.18.04.1

Reproduction Steps:

  1. Create a brand new Phabricator instance (e.g. by following the steps on https://github.com/sitsofe/phabricator-clone-hang ).
  2. Try and import libphutil:

# Create repository in Phabricator
echo '{"transactions": [{"type":"vcs", "value": "git"},
          {"type":"name", "value":"libphutil"},
          {"type":"publish", "value":false },
          {"type":"callsign", "value":"LIBPHUTIL"}]
      }' | /opt/arcanist/bin/arc call-conduit 'diffusion.repository.edit'
# Grab PHID for repo
REPO_PHID="$(echo '{ "names": [ "rLIBPHUTIL" ] }' | \
    arc call-conduit phid.lookup | jq -r '.response | .rLIBPHUTIL.phid')"
# Set all the existing repo URIs to read-only
URI_PHIDS="$(echo '{ "constraints": { "phids": [ "'"${REPO_PHID}"'" ] },
    "attachments": { "uris": true } }' | \
    arc call-conduit diffusion.repository.search | \
    jq -r '.response.data[0].attachments.uris.uris[].phid')"
for phid in ${URI_PHIDS}; do
    echo '{ "transactions": [ { "type": "io", "value": "read" } ],
        "objectIdentifier": "'"${phid}"'" }' | \
    arc call-conduit diffusion.uri.edit
done
# Add upstream URI (to import from)
echo '{ "transactions": [
    { "type": "repository", "value": "'"${REPO_PHID}"'" },
    { "type": "uri", "value": "https://secure.phabricator.com/source/libphutil.git" },
    { "type": "io", "value": "observe" }
  ]
}' | arc call-conduit diffusion.uri.edit
# Activate the repo (and start the import process)
echo '{
  "transactions": [ { "type": "status", "value": "active" } ],
  "objectIdentifier": "'"${REPO_PHID}"'"
}' | arc call-conduit diffusion.repository.edit

# Wait for the import to finish
echo "Waiting for libphutil import (this may take a while)..."
echo -n "(monitor progress via http://${SERVER_FQDN}/diffusion/LIBPHUTIL/ ):  "
sp="/-\\|"
while true; do
    # Check import state
    IMPORTING=$(echo '{ "constraints": { "callsigns": [ "LIBPHUTIL" ] }
    }' | arc call-conduit diffusion.repository.search | jq  -r '.response.data[0].fields.isImporting')
    if [[ "$IMPORTING" == "true" ]]; then
        printf "\b${sp:i++%${#sp}:1}"
        sleep 3;
    else
        break
    fi
done
printf '    \b\b\b\b\n'
echo 'Import complete!'

(Note: these errors only ever seem to turn up when importing a repository via conduit calls. I’ve tried multiple times “by-hand” and everything works fine)