Skip to content

statsig-node-core with data store prevents process exit #32

@jasperlin451

Description

@jasperlin451

Running this code with tsx or ts-node always results in hang. If you turn off the datastore, it stops hanging.

import {
  type DataStore,
  type DataStoreResponse,
  Statsig,
  type StatsigOptions,
} from '@statsig/statsig-node-core';

// Simple in-memory DataStore - no external dependencies
class InMemoryDataStore implements DataStore {
  private cache = new Map<string, { value: string; time?: number }>();

  async initialize(): Promise<void> {
    console.log('DataStore: initialized');
  }

  async shutdown(): Promise<void> {
    this.cache.clear();
    console.log('DataStore: shutdown');
  }

  // Arrow functions to preserve `this` binding (as recommended by Statsig docs)
  get = async (key: string): Promise<DataStoreResponse> => {
    const entry = this.cache.get(key);
    if (!entry) return {};
    return { result: entry.value, time: entry.time };
  };

  set = async (key: string, value: string, time?: number): Promise<void> => {
    this.cache.set(key, { value, time });
  };
}

async function testWithDataStore() {
  console.log('\n=== Test: Statsig WITH DataStore ===');

  const dataStore = new InMemoryDataStore();
  const options: StatsigOptions = { dataStore };
  const statsig = new Statsig('secret-test-key', options);

  console.log('Initializing...');
  await statsig.initialize();

  console.log('Shutting down Statsig...');
  await statsig.shutdown(1000);

  console.log('Shutting down DataStore...');
  await dataStore.shutdown();

  console.log('Done');
}

async function testWithoutDataStore() {
  console.log('\n=== Test: Statsig WITHOUT DataStore ===');

  const statsig = new Statsig('secret-test-key', {});

  console.log('Initializing...');
  await statsig.initialize();

  console.log('Shutting down...');
  await statsig.shutdown(1000);

  console.log('Done');
}

async function main() {
  // This hangs - process never exits:
  await testWithDataStore()

  // this works fine
  // await testWithoutDataStore(); 

  console.log('\nHandles keeping process alive:');

  console.log('\nProcess should exit now...');
}

main().catch(console.error);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions