Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(entitlements): enforce plan limits atomically on member and monitor creation (#16293)

Steffen Schmitz324fa3ae0359
diff --git a/packages/shared/src/features/monitors/service/service.ts b/packages/shared/src/features/monitors/service/service.ts
index bd5c96275b..e64a76b7cd 100644
--- a/packages/shared/src/features/monitors/service/service.ts
+++ b/packages/shared/src/features/monitors/service/service.ts
@@ -46,6 +46,12 @@ export class MonitorService {
public static async create(
session: SessionContext,
input: CreateMonitor,
+ /**
+ * Optional transaction client, so a caller can create the monitor inside
+ * its own transaction (e.g. to enforce a plan limit atomically). Defaults
+ * to the shared client.
+ */
+ tx: Prisma.TransactionClient = prisma,
): Promise<Monitor> {
const filters = sortFiltersCanonically(input.filters);
const windowMs = windowToMs(input.window);
@@ -57,7 +63,7 @@ export class MonitorService {
windowMs,
});
- const created = await prisma.monitor.create({
+ const created = await tx.monitor.create({
data: {
projectId: input.projectId,
createdBy: session.userId,
diff --git a/web/src/__tests__/server/members-trpc.servertest.ts b/web/src/__tests__/server/members-trpc.servertest.ts
index 2bdc80a970..fb56a7a7df 100644
--- a/web/src/__tests__/server/members-trpc.servertest.ts
+++ b/web/src/__tests__/server/members-trpc.servertest.ts
@@ -2,6 +2,7 @@ import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { prisma } from "@langfuse/shared/src/db";
import { Role, type Plan } from "@langfuse/shared";
+import { entitlementAccess } from "@/src/features/entitlements/constants/entitlements";
import type { Session } from "next-auth";
// Session fixture sub-object types; casts keep the runtime fixtures unchanged
@@ -125,6 +126,16 @@ async function createTestUser() {
describe("membersRouter.create - organization member limit enforcement", () => {
describe("cloud:hobby plan (2 member limit)", () => {
+ const hobbyMemberLimit =
+ entitlementAccess["cloud:hobby"].entitlementLimits[
+ "organization-member-count"
+ ];
+ if (typeof hobbyMemberLimit !== "number" || hobbyMemberLimit < 2) {
+ throw new Error(
+ "expected cloud:hobby organization-member-count to be a number >= 2; the concurrency tests need one free seat above the owner",
+ );
+ }
+
it("should allow adding a member when within limit", async () => {
const { org, caller } = await prepare("cloud:hobby");
@@ -173,6 +184,74 @@ describe("membersRouter.create - organization member limit enforcement", () => {
).rejects.toThrow(/exceeds the limit/i);
});
+ it("does not admit more members than the limit under concurrent requests", async () => {
+ // Counting outside a transaction lets concurrent requests all observe
+ // the same pre-insert count and all pass the check, so the limit only
+ // holds if the count-check-create sequence is serialized.
+ const { org, caller } = await prepare("cloud:hobby");
+
+ // Org already has 1 member (the owner), so exactly one of the
+ // concurrent requests may create a membership.
+ const users = await Promise.all(
+ Array.from({ length: 5 }, () => createTestUser()),
+ );
+
+ const results = await Promise.allSettled(
+ users.map((user) =>
+ caller.members.create({
+ orgId: org.id,
+ email: user.email!,
+ orgRole: Role.MEMBER,
+ }),
+ ),
+ );
+
+ const memberCount = await prisma.organizationMembership.count({
+ where: { orgId: org.id },
+ });
+ expect(memberCount).toBe(hobbyMemberLimit);
+
+ const rejected = results.filter((r) => r.status === "rejected");
+ expect(results.length - rejected.length).toBe(1);
+ // every loser lost to the limit check, not to an incidental error
+ for (const result of rejected) {
+ expect(String(result.reason)).toMatch(/exceeds the limit/i);
+ }
+ }, 25_000);
+
+ it("does not admit more invitations than the limit under concurrent requests", async () => {
+ // Same race on the invitation branch, which creates a different row
+ // type behind the same shared seat limit.
+ const { org, caller } = await prepare("cloud:hobby");
+
+ const emails = Array.from(
+ { length: 5 },
+ () => `concurrent-${uuidv4().substring(0, 8)}@test.com`,
+ );
+
+ const results = await Promise.allSettled(
+ emails.map((email) =>
+ caller.members.create({
+ orgId: org.id,
+ email,
+ orgRole: Role.MEMBER,
+ }),
+ ),
+ );
+
+ const inviteCount = await prisma.membershipInvitation.count({
+ where: { orgId: org.id },
+ });
+ // owner + 1 invitation = the hobby limit
+ expect(inviteCount).toBe(hobbyMemberLimit - 1);
+
+ const rejected = results.filter((r) => r.status === "rejected");
+ expect(results.length - rejected.length).toBe(1);
+ for (const result of rejected) {
+ expect(String(result.reason)).toMatch(/exceeds the limit/i);
+ }
+ }, 25_000);
+
it("should throw FORBIDDEN when exceeding member limit with pending invitations", async () => {
const { org, caller, ownerUser } = await prepare("cloud:hobby");
diff --git a/web/src/__tests__/server/monitors.servertest.ts b/web/src/__tests__/server/monitors.servertest.ts
index 76745c1e36..e06e8d93fe 100644
--- a/web/src/__tests__/server/monitors.servertest.ts
+++ b/web/src/__tests__/server/monitors.servertest.ts
@@ -457,6 +457,38 @@ describe("monitors trpc", () => {
).rejects.toThrow(/monitor-count/i);
});
+ it("does not admit more monitors than the limit under concurrent requests", async () => {
+ // Counting outside a transaction lets concurrent requests all observe
+ // the same pre-insert count and all pass the check, so the limit only
+ // holds if the count-check-create sequence is serialized.
+ const { project, caller } = await prepare();
+ // Seed up to one seat below the limit, so exactly one of the concurrent
+ // requests may succeed — otherwise a race that admits `monitorLimit`
+ // rows would satisfy the limit by coincidence.
+ await seedMonitors(caller, project.id, monitorLimit - 1);
+
+ const results = await Promise.allSettled(
+ Array.from({ length: 5 }, (_, i) =>
+ caller.monitors.create({
+ ...validMonitorInput(project.id),
+ name: `Concurrent monitor ${i + 1}`,
+ }),
+ ),
+ );
+
+ const created = await prisma.monitor.count({
+ where: { projectId: project.id },
+ });
+ expect(created).toBe(monitorLimit);
+
+ const rejected = results.filter((r) => r.status === "rejected");
+ expect(results.length - rejected.length).toBe(1);
+ // every loser lost to the limit check, not to an incidental error
+ for (const result of rejected) {
+ expect(String(result.reason)).toMatch(/monitor-count/i);
+ }
+ }, 25_000);
+
it("counts monitors with non-ACTIVE status toward the limit", async () => {
const { project, caller } = await prepare();
await seedMonitors(caller, project.id, monitorLimit);
diff --git a/web/src/features/entitlements/server/createWithinEntitlementLimit.ts b/web/src/features/entitlements/server/createWithinEntitlementLimit.ts
new file mode 100644
index 0000000000..d81181da2b
--- /dev/null
+++ b/web/src/features/entitlements/server/createWithinEntitlementLimit.ts
@@ -0,0 +1,80 @@
+import {
+ hasEntitlementLimit,
+ throwIfExceedsLimit,
+} from "@/src/features/entitlements/server/hasEntitlementLimit";
+import { type EntitlementLimit } from "@/src/features/entitlements/constants/entitlements";
+import { Prisma, type PrismaClient } from "@langfuse/shared/src/db";
+import { type Session } from "next-auth";
+
+/**
+ * Serializes the rest of the transaction against every other holder of this
+ * lock. Concurrent inserts of rows that reference the organization take
+ * FOR KEY SHARE on the same row via their foreign key, so they queue behind
+ * this lock too — keep the locked section to local database work only (no
+ * email, no outbound HTTP).
+ */
+async function lockOrganization({
+ tx,
+ orgId,
+}: {
+ tx: Prisma.TransactionClient;
+ orgId: string;
+}) {
+ await tx.$queryRaw(
+ Prisma.sql`
+ SELECT id
+ FROM organizations
+ WHERE id = ${orgId}
+ FOR UPDATE
+ `,
+ );
+}
+
+/**
+ * Creates a resource only if doing so keeps the organization within a numeric
+ * entitlement limit.
+ *
+ * Counting and creating in separate auto-committed statements is a
+ * time-of-check-to-time-of-use race: concurrent requests all read the same
+ * pre-insert count, all pass the check, and all insert — so a plan limit of N
+ * admits more than N rows. Serializing on the organization row means a
+ * request's count already includes every insert that committed before it, on
+ * a single instance and across instances alike.
+ *
+ * Orgs whose plan does not limit this entitlement skip the lock entirely, so
+ * unlimited plans keep creating resources concurrently.
+ */
+export async function createWithinEntitlementLimit<T>({
+ prisma,
+ orgId,
+ entitlementLimit,
+ sessionUser,
+ countCurrentUsage,
+ create,
+}: {
+ prisma: PrismaClient;
+ orgId: string;
+ entitlementLimit: EntitlementLimit;
+ sessionUser: NonNullable<Session["user"]>;
+ /** Counts existing usage towards the limit. Must read through `tx`. */
+ countCurrentUsage: (tx: Prisma.TransactionClient) => Promise<number>;
+ /** Creates the resource. Must write through `tx`. */
+ create: (tx: Prisma.TransactionClient) => Promise<T>;
+}): Promise<T> {
+ const isUnlimited =
+ hasEntitlementLimit({ entitlementLimit, sessionUser, orgId }) === false;
+ if (isUnlimited) return create(prisma);
+
+ return prisma.$transaction(async (tx) => {
+ await lockOrganization({ tx, orgId });
+
+ throwIfExceedsLimit({
+ entitlementLimit,
+ sessionUser,
+ orgId,
+ currentUsage: await countCurrentUsage(tx),
+ });
+
+ return create(tx);
+ });
+}
diff --git a/web/src/features/rbac/server/membersRouter.ts b/web/src/features/rbac/server/membersRouter.ts
index aba120ed6e..996c80d8f0 100644
--- a/web/src/features/rbac/server/membersRouter.ts
+++ b/web/src/features/rbac/server/membersRouter.ts
@@ -21,7 +21,7 @@ import { sendMembershipInvitationEmail } from "@langfuse/shared/src/server";
import { env } from "@/src/env.mjs";
import { getSfdcService } from "@/src/ee/features/sfdc-sync/server";
import { hasEntitlement } from "@/src/features/entitlements/server/hasEntitlement";
-import { throwIfExceedsLimit } from "@/src/features/entitlements/server/hasEntitlementLimit";
+import { createWithinEntitlementLimit } from "@/src/features/entitlements/server/createWithinEntitlementLimit";
import {
hasProjectAccess,
throwIfNoProjectAccess,
@@ -221,15 +221,18 @@ export const membersRouter = createTRPCRouter({
throw new TRPCError({ code: "NOT_FOUND", message: "Org not found" });
}
- // Count current members + pending invitations for limit check
- const [currentMemberCount, pendingInviteCount] = await Promise.all([
- ctx.prisma.organizationMembership.count({
+ // Members and pending invitations both consume a seat, so the limit
+ // check counts them together. Reads through the caller-supplied client
+ // so it can run inside the limit transaction.
+ const countSeatsInUse = async (tx: Prisma.TransactionClient) => {
+ const memberCount = await tx.organizationMembership.count({
where: { orgId: input.orgId },
- }),
- ctx.prisma.membershipInvitation.count({
+ });
+ const pendingInviteCount = await tx.membershipInvitation.count({
where: { orgId: input.orgId },
- }),
- ]);
+ });
+ return memberCount + pendingInviteCount;
+ };
if (user) {
const existingOrgMembership =
@@ -276,21 +279,22 @@ export const membersRouter = createTRPCRouter({
});
}
- // Check member limit before creating new membership
- throwIfExceedsLimit({
+ // create org membership as user is not a member yet, unless that
+ // would exceed the member limit
+ const orgMembership = await createWithinEntitlementLimit({
+ prisma: ctx.prisma,
+ orgId: input.orgId,
entitlementLimit: "organization-member-count",
sessionUser: ctx.session.user,
- orgId: input.orgId,
- currentUsage: currentMemberCount + pendingInviteCount,
- });
-
- // create org membership as user is not a member yet
- const orgMembership = await ctx.prisma.organizationMembership.create({
- data: {
- userId: user.id,
- orgId: input.orgId,
- role: input.orgRole,
- },
+ countCurrentUsage: countSeatsInUse,
+ create: (tx) =>
+ tx.organizationMembership.create({
+ data: {
+ userId: user.id,
+ orgId: input.orgId,
+ role: input.orgRole,
+ },
+ }),
});
await auditLog({
session: ctx.session,
@@ -334,30 +338,35 @@ export const membersRouter = createTRPCRouter({
env: env,
});
} else {
- // Check member limit before creating invitation
- throwIfExceedsLimit({
- entitlementLimit: "organization-member-count",
- sessionUser: ctx.session.user,
- orgId: input.orgId,
- currentUsage: currentMemberCount + pendingInviteCount,
- });
-
try {
- const invitation = await ctx.prisma.membershipInvitation.create({
- data: {
- orgId: input.orgId,
- projectId:
- project && input.projectRole && input.projectRole !== Role.NONE
- ? project.id
- : null,
- email: input.email.toLowerCase(),
- orgRole: input.orgRole,
- projectRole:
- input.projectRole && input.projectRole !== Role.NONE && project
- ? input.projectRole
- : null,
- invitedByUserId: ctx.session.user.id,
- },
+ // create the invitation unless that would exceed the member limit
+ const invitation = await createWithinEntitlementLimit({
+ prisma: ctx.prisma,
+ orgId: input.orgId,
+ entitlementLimit: "organization-member-count",
+ sessionUser: ctx.session.user,
+ countCurrentUsage: countSeatsInUse,
+ create: (tx) =>
+ tx.membershipInvitation.create({
+ data: {
+ orgId: input.orgId,
+ projectId:
+ project &&
+ input.projectRole &&
+ input.projectRole !== Role.NONE
+ ? project.id
+ : null,
+ email: input.email.toLowerCase(),
+ orgRole: input.orgRole,
+ projectRole:
+ input.projectRole &&
+ input.projectRole !== Role.NONE &&
+ project
+ ? input.projectRole
+ : null,
+ invitedByUserId: ctx.session.user.id,
+ },
+ }),
});
await auditLog({
diff --git a/web/src/server/api/routers/monitors.ts b/web/src/server/api/routers/monitors.ts
index db3caab316..4b9002daed 100644
--- a/web/src/server/api/routers/monitors.ts
+++ b/web/src/server/api/routers/monitors.ts
@@ -6,7 +6,7 @@ import {
requireV4Writes,
} from "@/src/server/api/trpc";
import { throwIfNoProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
-import { throwIfExceedsLimit } from "@/src/features/entitlements/server/hasEntitlementLimit";
+import { createWithinEntitlementLimit } from "@/src/features/entitlements/server/createWithinEntitlementLimit";
import {
CreateMonitorSchema,
DeleteMonitorSchema,
@@ -36,17 +36,18 @@ export const monitorsRouter = createTRPCRouter({
scope: "alerts:CUD",
});
- const currentCount = await ctx.prisma.monitor.count({
- where: { project: { orgId: ctx.session.orgId, deletedAt: null } },
- });
- throwIfExceedsLimit({
+ return createWithinEntitlementLimit({
+ prisma: ctx.prisma,
+ orgId: ctx.session.orgId,
entitlementLimit: "monitor-count",
sessionUser: ctx.session.user,
- orgId: ctx.session.orgId,
- currentUsage: currentCount,
+ countCurrentUsage: (tx) =>
+ tx.monitor.count({
+ where: { project: { orgId: ctx.session.orgId, deletedAt: null } },
+ }),
+ create: (tx) =>
+ MonitorService.create(sessionContextFromCtx(ctx), input, tx),
});
-
- return MonitorService.create(sessionContextFromCtx(ctx), input);
}),
update: monitorsProcedure