Skip to content

Commit

Permalink
Fix webhook application hang on exit
Browse files Browse the repository at this point in the history
* Import Lyncredible@d9fee18 manually
  • Loading branch information
abraunegg committed Oct 27, 2023
1 parent 16595ef commit 4df36ff
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/itemdb.d
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ final class ItemDatabase {
try {
auto stmt = db.prepare("VACUUM;");
stmt.exec();
log.vdebug("Database vacuum is complete");
} catch (SqliteException e) {
writeln();
log.error("ERROR: Unable to perform a database vacuum: " ~ e.msg);
Expand Down
37 changes: 27 additions & 10 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -898,39 +898,56 @@ int main(string[] cliArgs) {
void performStandardExitProcess(string scopeCaller) {
// Who called this function
log.vdebug("Running performStandardExitProcess due to: ", scopeCaller);

// Shutdown the database
if (itemDB !is null) {
// Make sure the .wal file is incorporated into the main db before we exit
itemDB.performVacuum();
object.destroy(itemDB);
}


// Wait for all parallel jobs that depend on the database to complete
log.vdebug("Wait for all parallel jobs that depend on the database to complete");
taskPool.finish(true);

// Shutdown the OneDrive API instance
if (oneDriveApiInstance !is null) {
log.vdebug("Shutdown OneDrive API instance");
oneDriveApiInstance.shutdown();
object.destroy(oneDriveApiInstance);
}

// Shutdown the sync engine
if (syncEngineInstance !is null) object.destroy(syncEngineInstance);
if (syncEngineInstance !is null) {
log.vdebug("Shutdown Sync Engine instance");
object.destroy(syncEngineInstance);
}

// Shutdown the client side filtering objects
if (selectiveSync !is null) object.destroy(selectiveSync);
if (selectiveSync !is null) {
log.vdebug("Shutdown Client Side Filtering instance");
object.destroy(selectiveSync);
}

// Shutdown the application configuration objects
if (appConfig !is null) {
log.vdebug("Shutdown Application Configuration instance");
// Cleanup any existing dry-run elements ... these should never be left hanging around
cleanupDryRunDatabaseFiles(appConfig.databaseFilePathDryRun);
object.destroy(appConfig);
}

// Shutdown any local filesystem monitoring
if (filesystemMonitor !is null) {
log.vdebug("Shutdown Filesystem Monitoring instance");
filesystemMonitor.shutdown();
object.destroy(filesystemMonitor);
}

// Shutdown the database
if (itemDB !is null) {
log.vdebug("Shutdown Database instance");
// Make sure the .wal file is incorporated into the main db before we exit
if (itemDB.isDatabaseInitialised()) {
itemDB.performVacuum();
}
object.destroy(itemDB);
}

// Set all objects to null
if (scopeCaller == "failureScope") {
// Set these to be null due to failure scope - prevent 'ERROR: Unable to perform a database vacuum: out of memory' when the exit scope is then called
log.vdebug("Setting Class Objects to null due to failure scope");
Expand Down
39 changes: 33 additions & 6 deletions src/onedrive.d
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class OneDriveWebhook {
private ushort port;
private Tid parentTid;
private shared uint count;
private bool started;

static OneDriveWebhook getOrCreate(string host, ushort port, Tid parentTid) {
if (!instantiated_) {
Expand All @@ -97,9 +98,23 @@ class OneDriveWebhook {
this.parentTid = parentTid;
this.count = 0;
}

void serve() {
spawn(&serveStatic);
this.started = true;
log.log("Started webhook server");
}

void stop() {
if (this.started) {
RequestServer.stop();
this.started = false;
}
log.log("Stopped webhook server");
}

// The static serve() is necessary because spawn() does not like instance methods
static serve() {
private static void serveStatic() {
// we won't create the singleton instance if it hasn't been created already
// such case is a bug which should crash the program and gets fixed
instance_.serveImpl();
Expand Down Expand Up @@ -458,9 +473,20 @@ class OneDriveApi {

// Shutdown OneDrive API Curl Engine
void shutdown() {

// Delete subscription if there exists any
deleteSubscription();

try {
deleteSubscription();
} catch (OneDriveException e) {
logSubscriptionError(e);
}

// Shutdown webhook server if it is running
if (webhook !is null) {
webhook.stop();
object.destroy(webhook);
}

// Reset any values to defaults, freeing any set objects
curlEngine.http.clearRequestHeaders();
curlEngine.http.onSend = null;
Expand Down Expand Up @@ -858,7 +884,7 @@ class OneDriveApi {
to!ushort(appConfig.getValueLong("webhook_listening_port")),
thisTid
);
spawn(&OneDriveWebhook.serve);
webhook.serve();
}

auto elapsed = Clock.currTime(UTC()) - subscriptionLastErrorAt;
Expand Down Expand Up @@ -999,13 +1025,14 @@ class OneDriveApi {

private void deleteSubscription() {
if (!hasValidSubscription()) {
log.vdebug("No valid Microsoft OneDrive webhook subscription to delete");
return;
}

string url;
url = subscriptionUrl ~ "/" ~ subscriptionId;
del(url);
log.log("Deleted subscription");
performDelete(url);
log.vdebug("Deleted Microsoft OneDrive webhook subscription");
}

private void logSubscriptionError(OneDriveException e) {
Expand Down

0 comments on commit 4df36ff

Please sign in to comment.