Skip to content

Commit

Permalink
Merge pull request #1368 from balena-io/abort-rollback
Browse files Browse the repository at this point in the history
Rollback device registration if the request ends/is aborted midway
  • Loading branch information
Page- authored Jun 27, 2023
2 parents f39debe + 828c5ae commit 7f3ad13
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/features/device-provisioning/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getDeviceTypeBySlug } from '../device-types/device-types';
import { checkInt } from '../../lib/utils';
import { checkDeviceExistsIsFrozen } from '../device-state/middleware';
import { gracefullyDenyConflictingRegistrations } from './gracefully-deny-conflicting-registrations';
import onFinished from 'on-finished';

const { BadRequestError, ConflictError } = errors;
const { api } = sbvrUtils;
Expand Down Expand Up @@ -67,6 +68,13 @@ export const register: RequestHandler = async (req, res) => {
);

const response = await sbvrUtils.db.transaction(async (tx) => {
// TODO: Replace this manual rollback on request closure with a more generic/automated version
onFinished(res, () => {
if (!tx.isClosed()) {
tx.rollback();
}
});

const resinApiTx = api.resin.clone({ passthrough: { req, tx } });
const deviceType = await getDeviceTypeBySlug(resinApiTx, deviceTypeSlug);
const device = await resinApiTx.post({
Expand Down

0 comments on commit 7f3ad13

Please sign in to comment.