Skip to content

Commit

Permalink
fix memory leak on SIGHUP
Browse files Browse the repository at this point in the history
  • Loading branch information
okimiko committed Jan 31, 2025
1 parent 1d60dd6 commit a245126
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/serve_light.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const serve_rendered = {
init: (options, repo, programOpts) => {},
add: (options, repo, params, id, programOpts, dataResolver) => {},
remove: (repo, id) => {},
clear: (repo) => {},
getTerrainElevation: (data, param) => {
param['elevation'] = 'not supported in light';
return param;
Expand Down
21 changes: 19 additions & 2 deletions src/serve_rendered.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import '@maplibre/maplibre-gl-native';
import advancedPool from 'advanced-pool';
import path from 'path';
import url from 'url';
import util from 'util';
import sharp from 'sharp';
import clone from 'clone';
import Color from 'color';
Expand Down Expand Up @@ -1458,7 +1457,25 @@ export const serve_rendered = {
}
delete repo[id];
},

/**
* Removes all items from the repository.
* @param {object} repo Repository object.
* @returns {void}
*/
clear: function (repo) {
Object.keys(repo).forEach((id) => {
const item = repo[id];
if (item) {
item.map.renderers.forEach((pool) => {
pool.close();
});
item.map.renderersStatic.forEach((pool) => {
pool.close();
});
}
delete repo[id];
});
},
/**
* Get the elevation of terrain tile data by rendering it to a canvas image
* @param {object} data The background color (or empty string for transparent).
Expand Down
6 changes: 6 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ async function start(opts) {
app,
server,
startupPromise,
serving,
};
}
/**
Expand Down Expand Up @@ -777,8 +778,13 @@ export async function server(opts) {

running.server.shutdown(async () => {
const restarted = await start(opts);
if (!isLight) {
serve_rendered.clear(running.serving.rendered);
}
running.server = restarted.server;
running.app = restarted.app;
running.startupPromise = restarted.startupPromise;
running.serving = restarted.serving;
});
});
return running;
Expand Down

0 comments on commit a245126

Please sign in to comment.