Skip to content

Commit 59da4d5

Browse files
committed
fix spend limit compute server modal crash bug
1 parent 3d7537a commit 59da4d5

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

src/packages/frontend/compute/compute-server.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Button, Card, Divider, Modal, Popconfirm, Spin } from "antd";
2-
import { CSSProperties, useMemo, useState } from "react";
3-
4-
import { useTypedRedux } from "@cocalc/frontend/app-framework";
2+
import { CSSProperties, useEffect, useMemo, useState } from "react";
3+
import { redux, useTypedRedux } from "@cocalc/frontend/app-framework";
54
import { Icon } from "@cocalc/frontend/components";
65
import ShowError from "@cocalc/frontend/components/error";
76
import { CancelText } from "@cocalc/frontend/i18n/components";
@@ -564,6 +563,13 @@ export default function ComputeServer({
564563
}
565564

566565
export function useServer({ id, project_id }) {
566+
useEffect(() => {
567+
const actions = redux.getProjectActions(project_id);
568+
actions.incrementReferenceCount();
569+
return () => {
570+
actions.decrementReferenceCount();
571+
};
572+
}, [project_id]);
567573
const computeServers = useTypedRedux({ project_id }, "compute_servers");
568574
const server = useMemo(() => {
569575
return computeServers?.get(`${id}`)?.toJS();

src/packages/frontend/compute/spend-limit.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ export function SpendLimit({
4444
const [error, setError] = useState<string>("");
4545
const [saving, setSaving] = useState<boolean>(false);
4646
const [spendLimit, setSpendLimit] = useState<Partial<ISpendLimit>>(
47-
server.configuration?.spendLimit ?? SPEND_LIMIT_DEFAULTS,
47+
server?.configuration?.spendLimit ?? SPEND_LIMIT_DEFAULTS,
4848
);
49+
useEffect(() => {
50+
setSpendLimit(server?.configuration?.spendLimit ?? SPEND_LIMIT_DEFAULTS);
51+
}, [server?.configuration]);
4952

5053
if (server == null) {
5154
return <Spin />;
@@ -197,6 +200,7 @@ export function SpendLimitModal({ id, project_id, close, extra = [] }) {
197200
</div>
198201
}
199202
>
203+
{help && <div>Configure limits on spending here.</div>}
200204
<SpendLimit id={id} project_id={project_id} help={help} extra={extra} />
201205
</Modal>
202206
);

src/packages/frontend/project_actions.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,31 @@ export class ProjectActions extends Actions<ProjectStoreState> {
336336
};
337337
isTabClosed = () => !this.isTabOpened();
338338

339+
// The expensive part of the actions should NOT exist if and only if
340+
// the reference count is 0 *AND* the tab is closed.
341+
private referenceCount = 0;
342+
343+
incrementReferenceCount = () => {
344+
this.referenceCount++;
345+
this.initExpensive();
346+
};
347+
348+
decrementReferenceCount = () => {
349+
if (this.referenceCount <= 0) {
350+
console.warn(
351+
"BUG: attempt to decrement project actions reference count below 0",
352+
);
353+
return;
354+
}
355+
this.referenceCount--;
356+
if (this.referenceCount <= 0 && !this.isTabOpened()) {
357+
this.closeExpensive();
358+
}
359+
};
360+
339361
private expensiveLoop = async () => {
340362
while (this.state != "closed") {
341-
if (this.isTabOpened()) {
363+
if (this.isTabOpened() || this.referenceCount > 0) {
342364
this.initExpensive();
343365
} else {
344366
this.closeExpensive();

src/packages/util/smc-version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/* autogenerated by the update_version script */
2-
exports.version=1758034770;
2+
exports.version=1758657266;

0 commit comments

Comments
 (0)