Skip to content

TagSpaces is an offline, open source, document manager with tagging support

add abort request on WS (#2366)

Stanimir98efdf050ad5
diff --git a/src/main/main.ts b/src/main/main.ts
index 153fc307a..3f865dd1c 100644
--- a/src/main/main.ts
+++ b/src/main/main.ts
@@ -840,12 +840,45 @@ app
ipcMain.on('relaunch-app', reloadApp);
+ process.removeAllListeners('uncaughtException'); // app crashes on ECONNRESET in default uncaughtException of Electron
+
process.on('uncaughtException', (error) => {
- if (error.stack) {
- console.error('error:', error.stack);
- throw new Error(error.stack);
+ // Always log the full error for diagnostics
+ console.error(
+ 'UNCAUGHT EXCEPTION in main:',
+ error && error.stack ? error.stack : error,
+ );
+
+ // Normalize message/code for checks
+ const msg = error && error.message ? error.message : '';
+ //@ts-ignore
+ const code = error && error.code ? error.code : '';
+
+ // Ignore or quietly handle known/non-fatal errors
+ const isAbort = error && error.name === 'AbortError';
+ const isSocketHangUp =
+ msg.includes('socket hang up') ||
+ code === 'ECONNRESET' ||
+ code === 'ECONNABORTED';
+
+ if (isAbort || isSocketHangUp) {
+ // Expected when cancelling requests — just log and continue.
+ console.warn(
+ 'Known non-fatal error (ignored):',
+ msg || code || error,
+ );
+ return;
+ } // For other errors: try graceful recovery (reload) or exit gracefully.
+ try {
+ reloadApp();
+ } catch (reloadErr) {
+ console.error(
+ 'reloadApp() failed, exiting process:',
+ reloadErr && reloadErr.stack ? reloadErr.stack : reloadErr,
+ );
+ // Exit with non-zero code to indicate failure (or perform other cleanup)
+ process.exit(1);
}
- reloadApp();
});
});
})
diff --git a/src/main/mainEvents.ts b/src/main/mainEvents.ts
index a77d6be93..08be1100f 100644
--- a/src/main/mainEvents.ts
+++ b/src/main/mainEvents.ts
@@ -48,6 +48,7 @@ import { ApiResponse } from './types';
//let watcher: FSWatcher;
const progress = {};
let wsc;
+const controllers = new Map(); // requestId -> AbortController
function isSafePath(filePath) {
if (typeof filePath !== 'string') return false;
@@ -206,13 +207,36 @@ export default function loadMainEvents() {
icon: icon,
});
});
- ipcMain.handle('postRequest', async (event, payload, endpoint) => {
+ ipcMain.handle('postRequest', async (event, payload, endpoint, requestId) => {
+ let controller;
+ if (requestId) {
+ controller = new AbortController();
+ controllers.set(requestId, controller);
+ }
try {
- const result = await postRequest(payload, endpoint);
+ const result = await postRequest(payload, endpoint, controller?.signal);
return result;
- } catch (e) {
- console.error(e);
+ } catch (err) {
+ console.error('postRequest error:', err);
+ if (
+ err &&
+ (err.name === 'AbortError' ||
+ err.message === 'Request aborted by signal')
+ ) {
+ return { error: 'AbortError' };
+ }
return false;
+ } finally {
+ // ensure controller cleaned up no matter what
+ if (requestId) controllers.delete(requestId);
+ }
+ });
+ // listen for cancel requests from renderer
+ ipcMain.on('cancelRequest', (event, requestId) => {
+ const controller = controllers.get(requestId);
+ if (controller) {
+ controller.abort();
+ controllers.delete(requestId);
}
});
ipcMain.handle(
diff --git a/src/main/preload.ts b/src/main/preload.ts
index bb1ee8d9b..4b52ca03d 100644
--- a/src/main/preload.ts
+++ b/src/main/preload.ts
@@ -79,7 +79,8 @@ export type Channels =
| 'newOllamaMessage'
| 'pullOllamaModel'
| 'deleteOllamaModel'
- | 'startup-finished';
+ | 'startup-finished'
+ | 'cancelRequest';
const electronHandler = {
ipcRenderer: {
diff --git a/src/main/util.ts b/src/main/util.ts
index a9497bddb..aa4e89845 100644
--- a/src/main/util.ts
+++ b/src/main/util.ts
@@ -19,14 +19,26 @@ export function resolveHtmlPath(htmlFileName: string) {
/**
* @param payload: string
* @param endpoint: string
+ * @param signal
*/
-export function postRequest(payload, endpoint) {
+export function postRequest(payload, endpoint, signal = undefined) {
return isWorkerAvailable().then((workerAvailable) => {
if (!workerAvailable) {
return Promise.reject(new Error('no Worker Available!'));
}
+
return new Promise((resolve, reject) => {
- const option = {
+ // quick abort check
+ if (signal && signal.aborted) {
+ const e = new Error('Aborted before sending');
+ e.name = 'AbortError';
+ return reject(e);
+ }
+
+ let settled = false; // ensure we resolve/reject only once
+ let respRef = null; // keep response to remove listeners on cleanup
+
+ const options = {
hostname: '127.0.0.1',
port: settings.getUsedWsPort(),
method: 'POST',
@@ -37,35 +49,116 @@ export function postRequest(payload, endpoint) {
'Content-Length': Buffer.byteLength(payload, 'utf8'),
},
};
- const reqPost = http
- .request(option, (resp) => {
- // .get('http://127.0.0.1:8888/thumb-gen?' + search.toString(), resp => {
- let data = '';
-
- // A chunk of data has been received.
- resp.on('data', (chunk) => {
- data += chunk;
- });
-
- // The whole response has been received. Print out the result.
- resp.on('end', () => {
- if (data) {
- try {
- resolve(JSON.parse(data));
- } catch (ex) {
- reject(ex);
- }
- } else {
- reject(new Error('Error: no data'));
+ const reqPost = http.request(options, (resp) => {
+ respRef = resp;
+ let data = '';
+
+ const onData = (chunk) => {
+ data += chunk;
+ };
+
+ // The whole response has been received. Print out the result.
+ const onEnd = () => {
+ if (settled) return;
+ settled = true;
+ cleanup();
+ if (data) {
+ try {
+ resolve(JSON.parse(data));
+ } catch (ex) {
+ reject(ex);
}
- });
- })
- .on('error', (err) => {
- console.log('Error: ' + err.message);
+ } else {
+ reject(new Error('Error: no data'));
+ }
+ };
+
+ const onRespError = (err) => {
+ if (settled) return;
+ settled = true;
+ cleanup();
reject(err);
- });
- reqPost.write(payload);
- reqPost.end();
+ };
+
+ resp.on('data', onData);
+ resp.on('end', onEnd);
+ resp.on('error', onRespError);
+ });
+
+ // handle request-level errors
+ const onReqError = (err) => {
+ if (settled) return;
+ // If the signal caused abort, we'll already reject from the abort handler.
+ // But http.request abort may also emit an 'error' (ECONNRESET). If signal was aborted,
+ // map this to AbortError for consistency.
+ if (signal && signal.aborted) {
+ const e = new Error('Request aborted');
+ e.name = 'AbortError';
+ settled = true;
+ cleanup();
+ return reject(e);
+ }
+ settled = true;
+ cleanup();
+ reject(err);
+ };
+
+ reqPost.on('error', onReqError);
+
+ // cleanup removes listeners
+ const cleanup = () => {
+ if (signal && typeof signal.removeEventListener === 'function') {
+ try {
+ signal.removeEventListener('abort', onSignalAbort);
+ } catch (ex) {}
+ }
+ reqPost.removeListener('error', onReqError);
+ if (respRef) {
+ respRef.removeAllListeners('data');
+ respRef.removeAllListeners('end');
+ respRef.removeAllListeners('error');
+ }
+ };
+
+ // abort handler
+ const onSignalAbort = () => {
+ if (settled) return;
+ // mark settled first to avoid race with req 'error' emission
+ settled = true;
+ cleanup();
+
+ // destroy/abort the underlying request. We don't pass an Error here
+ // because it may synchronously emit 'error' events that could be
+ // reentrant; we've already set settled and removed listeners.
+ try {
+ reqPost.destroy();
+ } catch (ex) {
+ /* ignore */
+ }
+
+ const abortErr = new Error('Request aborted by signal');
+ abortErr.name = 'AbortError';
+ reject(abortErr);
+ };
+
+ // attach abort listener once
+ if (signal && typeof signal.addEventListener === 'function') {
+ // use { once: true } so handler runs only once
+ signal.addEventListener('abort', onSignalAbort, { once: true });
+ }
+
+ // send payload
+ try {
+ reqPost.write(payload);
+ reqPost.end();
+ } catch (err) {
+ // synchronous error writing the payload
+ if (!settled) {
+ settled = true;
+ cleanup();
+ reject(err);
+ }
+ }
});
});
}
diff --git a/src/renderer/containers/PageNotification.tsx b/src/renderer/containers/PageNotification.tsx
index 81b4a639d..9f145f25d 100644
--- a/src/renderer/containers/PageNotification.tsx
+++ b/src/renderer/containers/PageNotification.tsx
@@ -22,7 +22,6 @@ import TsIconButton from '-/components/TsIconButton';
import { useLocationIndexContext } from '-/hooks/useLocationIndexContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
import { getLastPublishedVersion } from '-/reducers/settings';
-import i18n from '-/services/i18n';
import { openURLExternally } from '-/services/utils-io';
import CloseIcon from '@mui/icons-material/Close';
import Snackbar from '@mui/material/Snackbar';
@@ -37,6 +36,7 @@ import {
AppDispatch,
isUpdateAvailable,
} from '../reducers/app';
+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
const TSNotification = styled(Snackbar)(({ theme }) => {
return {
@@ -58,6 +58,7 @@ function PageNotification() {
showNotification,
hideNotifications,
} = useNotificationContext();
+ const { findLocation } = useCurrentLocationContext();
const { isIndexing, cancelDirectoryIndexing } = useLocationIndexContext();
const updateAvailable = useSelector(isUpdateAvailable);
const lastPublishedVersion = useSelector(getLastPublishedVersion);
@@ -122,12 +123,12 @@ function PageNotification() {
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
open={isIndexing !== undefined}
autoHideDuration={undefined}
- message={i18n.t('indexing') + ': ' + isIndexing}
+ message={t('indexing') + ': ' + findLocation(isIndexing)?.name}
action={[
<TsButton
key="cancelIndexButton"
color="secondary"
- onClick={() => cancelDirectoryIndexing()}
+ onClick={() => cancelDirectoryIndexing(isIndexing)}
data-tid="cancelDirectoryIndexing"
>
{t('core:cancelIndexing')}
diff --git a/src/renderer/hooks/CurrentLocationContextProvider.tsx b/src/renderer/hooks/CurrentLocationContextProvider.tsx
index c861a5f5b..09a7fe1f9 100644
--- a/src/renderer/hooks/CurrentLocationContextProvider.tsx
+++ b/src/renderer/hooks/CurrentLocationContextProvider.tsx
@@ -382,7 +382,10 @@ export const CurrentLocationContextProvider = ({
(l) => l.uuid !== locationId,
);*/
dispatch(LocationActions.deleteLocation(locationId));
- forceUpdate();
+ if (currentLocationId.current === locationId) {
+ setCurrentLocation(undefined);
+ }
+ //forceUpdate();
}
function moveLocationUp(locationUUID) {
diff --git a/src/renderer/hooks/DirectoryContentContextProvider.tsx b/src/renderer/hooks/DirectoryContentContextProvider.tsx
index f8a3f2f81..f0a514f37 100644
--- a/src/renderer/hooks/DirectoryContentContextProvider.tsx
+++ b/src/renderer/hooks/DirectoryContentContextProvider.tsx
@@ -808,7 +808,7 @@ export const DirectoryContentContextProvider = ({
entries: TS.FileSystemEntry[],
): TS.OrderVisibilitySettings[] {
return entries
- .filter((entry) => !entry.isFile)
+ .filter((entry) => entry && !entry.isFile)
.slice(0, defaultColumnsToShow)
.map((dir) => ({
uuid: dir.uuid,
diff --git a/src/renderer/hooks/LocationIndexContextProvider.tsx b/src/renderer/hooks/LocationIndexContextProvider.tsx
index 0748844fc..932cce994 100644
--- a/src/renderer/hooks/LocationIndexContextProvider.tsx
+++ b/src/renderer/hooks/LocationIndexContextProvider.tsx
@@ -55,7 +55,7 @@ type LocationIndexContextData = {
isIndexing: string;
getIndex: () => TS.FileSystemEntry[];
getLastIndex: (locationId: string) => Promise<TS.FileSystemEntry[]>;
- cancelDirectoryIndexing: () => void;
+ cancelDirectoryIndexing: (locationId: string) => void;
createLocationIndex: (
location: CommonLocation,
force?: boolean,
@@ -80,7 +80,7 @@ export const LocationIndexContext = createContext<LocationIndexContextData>({
isIndexing: undefined,
getIndex: undefined,
getLastIndex: undefined,
- cancelDirectoryIndexing: () => {},
+ cancelDirectoryIndexing: undefined,
createLocationIndex: () => Promise.resolve(false),
createLocationsIndexes: () => Promise.resolve(false),
clearDirectoryIndex: () => {},
@@ -101,8 +101,13 @@ export const LocationIndexContextProvider = ({
}: LocationIndexContextProviderProps) => {
const { t } = useTranslation();
- const { locations, findLocation, currentLocationId, getLocationPath } =
- useCurrentLocationContext();
+ const {
+ locations,
+ findLocation,
+ currentLocationId,
+ currentLocation,
+ getLocationPath,
+ } = useCurrentLocationContext();
const { ignoreByWatcher, deignoreByWatcher } = useFSWatcherContext();
const { setSearchResults, appendSearchResults, updateCurrentDirEntries } =
useDirectoryContentContext();
@@ -115,20 +120,29 @@ export const LocationIndexContextProvider = ({
//const allLocations = useSelector(getLocations);
const isIndexing = useRef<string>(undefined);
- let walking = true;
+ const walkingRef = useRef(true);
//const lastError = useRef(undefined);
const index = useRef<TS.FileSystemEntry[]>(undefined);
const indexLoadedOn = useRef<number>(undefined);
const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
const firstRender = useFirstRender();
- const currentLocation = findLocation();
const maxIndexAge = useRef<number>(getMaxIndexAge(currentLocation));
+ const prevLocationId = useRef<string>(currentLocationId);
useEffect(() => {
if (currentLocationId) {
clearDirectoryIndex(false);
maxIndexAge.current = getMaxIndexAge(findLocation());
}
+ if (AppConfig.isElectron && prevLocationId.current !== currentLocationId) {
+ if (prevLocationId.current) {
+ window.electronIO.ipcRenderer.sendMessage(
+ 'cancelRequest',
+ prevLocationId.current,
+ );
+ }
+ prevLocationId.current = currentLocationId;
+ }
}, [currentLocationId]);
useEffect(() => {
@@ -336,13 +350,16 @@ export const LocationIndexContextProvider = ({
}
function isWalking() {
- return walking;
+ return walkingRef.current;
}
- function cancelDirectoryIndexing() {
- walking = false;
- isIndexing.current = undefined;
- forceUpdate();
+ function cancelDirectoryIndexing(locationId: string) {
+ if (locationId) {
+ window.electronIO.ipcRenderer.sendMessage('cancelRequest', locationId);
+ walkingRef.current = false;
+ isIndexing.current = undefined;
+ forceUpdate();
+ }
}
function createDirectoryIndex(
@@ -374,11 +391,15 @@ export const LocationIndexContextProvider = ({
extractText,
!!loc.extractLinks,
ignorePatterns,
+ loc.uuid,
)
.then((result) => {
if (result && result.success) {
return loadIndexFromDisk(dirPath, param.locationID);
} else if (result && result.error) {
+ if (result.error === 'AbortError') {
+ return undefined;
+ }
console.error(
'createDirectoryIndexInWorker failed:' + result.error,
);
@@ -485,6 +506,7 @@ export const LocationIndexContextProvider = ({
location: CommonLocation,
force = false,
): Promise<boolean> {
+ walkingRef.current = true;
if (location) {
if (location.disableIndexing) {
if (force) {
@@ -541,11 +563,12 @@ export const LocationIndexContextProvider = ({
}
async function createLocationsIndexes(extractText = true): Promise<boolean> {
+ walkingRef.current = true;
for (let location of locations) {
try {
if (!location.disableIndexing) {
const locationPath = await getLocationPath(location);
- isIndexing.current = locationPath;
+ isIndexing.current = location.uuid; //locationPath
forceUpdate();
await createDirectoryIndexWrapper(
{ path: locationPath, locationID: location.uuid },
@@ -674,7 +697,7 @@ export const LocationIndexContextProvider = ({
}
function searchLocationIndex(searchQuery: TS.SearchQuery) {
- walking = true;
+ walkingRef.current = true;
if (!currentLocation) {
//showNotification(t('core:pleaseOpenLocation'), 'warning', true);
searchAllLocations(searchQuery);
@@ -740,7 +763,7 @@ export const LocationIndexContextProvider = ({
setSearchResults([]);
showNotification(t('core:searching'), 'default', false, 'TIDSearching');
- walking = true;
+ walkingRef.current = true;
//let searchResultCount = 0;
let searchResults = [];
let maxSearchResultReached = false;
@@ -834,17 +857,18 @@ export const LocationIndexContextProvider = ({
} else {
directoryPath = param;
}
+ const cLocation = findLocation(param?.locationID);
const metaDirectory = getMetaDirectoryPath(directoryPath);
- const exist = await currentLocation.checkDirExist(metaDirectory);
+ const exist = await cLocation.checkDirExist(metaDirectory);
try {
if (!exist) {
- await currentLocation.createDirectoryPromise(metaDirectory); // todo platformFacade?
+ await cLocation.createDirectoryPromise(metaDirectory); // todo platformFacade?
}
const folderIndexPath =
metaDirectory +
- currentLocation?.getDirSeparator() +
+ cLocation?.getDirSeparator() +
AppConfig.folderIndexFile; // getMetaIndexFilePath(directoryPath);
- return currentLocation
+ return cLocation
.saveTextFilePromise(
{ ...param, path: folderIndexPath },
JSON.stringify(directoryIndex), // relativeIndex),
diff --git a/src/renderer/utils/CommonLocation.ts b/src/renderer/utils/CommonLocation.ts
index bb8114696..c682eb4b4 100644
--- a/src/renderer/utils/CommonLocation.ts
+++ b/src/renderer/utils/CommonLocation.ts
@@ -982,6 +982,7 @@ export class CommonLocation implements TS.Location {
extractText: boolean,
extractLinks: boolean,
ignorePatterns: Array<string>,
+ requestId: string,
): Promise<any> => {
/*if (this.isReadOnly) {
return Promise.reject(new Error('read only Location'));
@@ -1001,6 +1002,7 @@ export class CommonLocation implements TS.Location {
'postRequest',
payload,
'/indexer',
+ requestId,
);
}
return Promise.reject(
diff --git a/tests/e2e/general.pw.e2e.js b/tests/e2e/general.pw.e2e.js
index a71456f72..42651a7de 100644
--- a/tests/e2e/general.pw.e2e.js
+++ b/tests/e2e/general.pw.e2e.js
@@ -246,16 +246,20 @@ test.describe('TST51 - Perspective Grid', () => {
test('TST0516 - Generate thumbnail from PDF [electron,s3]', async () => {
await expectMetaFilesExist(['sample.pdf.jpg']);
});
-
- test('TST0517 - Generate thumbnail from ODT [electron,minio,s3,_pro]', async () => {
+ /**
+ * todo not generated on S3 location
+ */
+ test('TST0517 - Generate thumbnail from ODT [electron,minio,_pro]', async () => {
await expectMetaFilesExist([
'sample.odt.jpg',
'sample.ods.jpg',
'sample.epub.jpg',
]);
});
-
- test('TST0519 - Generate thumbnail from TIFF [electron,minio,s3,_pro]', async () => {
+ /**
+ * todo not generated on S3 location
+ */
+ test('TST0519 - Generate thumbnail from TIFF [electron,minio,_pro]', async () => {
await expectMetaFilesExist(['sample.tiff.jpg']);
});
diff --git a/tests/e2e/perspective-kanban.pw.e2e.js b/tests/e2e/perspective-kanban.pw.e2e.js
index d3318fe9a..fd1a70f37 100644
--- a/tests/e2e/perspective-kanban.pw.e2e.js
+++ b/tests/e2e/perspective-kanban.pw.e2e.js
@@ -437,9 +437,13 @@ test.describe('TST49 - Perspective KanBan', () => {
await clickOn('[data-tid=changeThumbnailTID]');
await clickOn('[data-tid=predefinedThumbnailsTID] > li');
await clickOn('[data-tid=confirmCustomThumb]');
-
- const initColumnThumbStyle = await getAttribute(
+ /* const initColumnThumbStyle = await getAttribute(
+ '[data-tid=' + columnName + 'KanBanColumnThumbTID]',
+ 'style',
+ );*/
+ const thumbColumnThumbStyle = await waitUntilChanged(
'[data-tid=' + columnName + 'KanBanColumnThumbTID]',
+ undefined, //initColumnThumbStyle,
'style',
);
@@ -448,11 +452,11 @@ test.describe('TST49 - Perspective KanBan', () => {
await clickOn('[data-tid=confirmCustomThumb]');
const newColumnThumbStyle = await waitUntilChanged(
'[data-tid=' + columnName + 'KanBanColumnThumbTID]',
- initColumnThumbStyle,
+ thumbColumnThumbStyle,
'style',
);
- expect(initColumnThumbStyle).not.toBe(newColumnThumbStyle);
+ expect(thumbColumnThumbStyle).not.toBe(newColumnThumbStyle);
});
/**