From e7748a2f280a61247765324ea21265b254594103 Mon Sep 17 00:00:00 2001 From: nulmete Date: Wed, 18 Feb 2026 10:14:01 -0300 Subject: [PATCH 1/2] Log error when host_script_result record is not found --- server/datastore/mysql/scripts.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server/datastore/mysql/scripts.go b/server/datastore/mysql/scripts.go index e6cfa5b9924..21617cede49 100644 --- a/server/datastore/mysql/scripts.go +++ b/server/datastore/mysql/scripts.go @@ -456,7 +456,13 @@ func (ds *Datastore) getHostScriptExecutionResultDB(ctx context.Context, q sqlx. // try with upcoming activities err = sqlx.GetContext(ctx, q, &result, getUpcomingStmt, execID) if errors.Is(err, sql.ErrNoRows) { - return nil, ctxerr.Wrap(ctx, notFound("HostScriptResult").WithName(execID)) + // Log instead of returning an error to avoid breaking the host + // details page when there's an orphan reference in the database + // (e.g. host_mdm_actions pointing to a deleted host_script_results row). + // To fix the orphan, run: + // UPDATE host_mdm_actions SET unlock_ref = NULL WHERE unlock_ref = ''; + level.Error(ds.logger).Log("msg", "orphan script execution reference, HostScriptResult not found", "execution_id", execID) + return nil, nil } } if err != nil { From 8a46abfd71dc21225931edc6f8acaca8241154b2 Mon Sep 17 00:00:00 2001 From: nulmete Date: Wed, 18 Feb 2026 10:23:53 -0300 Subject: [PATCH 2/2] tweak logging --- server/datastore/mysql/scripts.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/server/datastore/mysql/scripts.go b/server/datastore/mysql/scripts.go index 21617cede49..ae3a8ed40ef 100644 --- a/server/datastore/mysql/scripts.go +++ b/server/datastore/mysql/scripts.go @@ -456,13 +456,7 @@ func (ds *Datastore) getHostScriptExecutionResultDB(ctx context.Context, q sqlx. // try with upcoming activities err = sqlx.GetContext(ctx, q, &result, getUpcomingStmt, execID) if errors.Is(err, sql.ErrNoRows) { - // Log instead of returning an error to avoid breaking the host - // details page when there's an orphan reference in the database - // (e.g. host_mdm_actions pointing to a deleted host_script_results row). - // To fix the orphan, run: - // UPDATE host_mdm_actions SET unlock_ref = NULL WHERE unlock_ref = ''; - level.Error(ds.logger).Log("msg", "orphan script execution reference, HostScriptResult not found", "execution_id", execID) - return nil, nil + return nil, ctxerr.Wrap(ctx, notFound("HostScriptResult").WithName(execID)) } } if err != nil { @@ -1532,17 +1526,23 @@ func (ds *Datastore) GetHostLockWipeStatus(ctx context.Context, host *fleet.Host // lock and unlock references are scripts if mdmActions.LockRef != nil { hsr, err := ds.getHostScriptExecutionResultDB(ctx, ds.reader(ctx), *mdmActions.LockRef, scriptExecutionSearchOpts{IncludeCanceled: true}) - if err != nil { + if err != nil && !fleet.IsNotFound(err) { return nil, ctxerr.Wrap(ctx, err, "get lock reference script result") } + if fleet.IsNotFound(err) { + level.Error(ds.logger).Log("msg", "orphan lock script execution reference", "host_id", host.ID, "execution_id", *mdmActions.LockRef) + } status.LockScript = hsr } if mdmActions.UnlockRef != nil { hsr, err := ds.getHostScriptExecutionResultDB(ctx, ds.reader(ctx), *mdmActions.UnlockRef, scriptExecutionSearchOpts{IncludeCanceled: true}) - if err != nil { + if err != nil && !fleet.IsNotFound(err) { return nil, ctxerr.Wrap(ctx, err, "get unlock reference script result") } + if fleet.IsNotFound(err) { + level.Error(ds.logger).Log("msg", "orphan unlock script execution reference", "host_id", host.ID, "execution_id", *mdmActions.UnlockRef) + } status.UnlockScript = hsr } @@ -1557,9 +1557,12 @@ func (ds *Datastore) GetHostLockWipeStatus(ctx context.Context, host *fleet.Host status.WipeMDMCommandResult = cmdRes } else { hsr, err := ds.getHostScriptExecutionResultDB(ctx, ds.reader(ctx), *mdmActions.WipeRef, scriptExecutionSearchOpts{IncludeCanceled: true}) - if err != nil { + if err != nil && !fleet.IsNotFound(err) { return nil, ctxerr.Wrap(ctx, err, "get wipe reference script result") } + if fleet.IsNotFound(err) { + level.Error(ds.logger).Log("msg", "orphan wipe script execution reference", "host_id", host.ID, "execution_id", *mdmActions.WipeRef) + } status.WipeScript = hsr } }