Problem stopping custom published method

I’m using a publish method that creates a custom cursor and observes its changes. Now, this published method is constantly being called, in order to change the filters I need to use in a very large collection, according to what the user requests.

This is my method:

Meteor.publish("filtered_vehicles_in_table", function(filters, order, start,
  rows, module, submodule, directFilters) {
  var user = null;
  if(this.userId) {
    var user = Meteor.users.findOne(this.userId);
  }

  var self = this;

  var cursor = vehicleQueries.getVehiclesForTable(filters, order, start, rows,
    user, module, submodule, directFilters);
  var handle = cursor
    .observe({
      added: function(item) {
        self.added('filtered_vehicles_in_table', item._id, item);
      },
      changed: function(item) {
        self.changed('filtered_vehicles_in_table', item._id, item);
      },
      removed: function(item) {
        self.removed('filtered_vehicles_in_table', item._id);
      }
    });

  this.ready();

  this.onStop(function() {
    handle.stop();
    console.log(handle);
  });
});

When i subscribe to this method, I store the handler in a local variable, and when I stop it, and then rebuild the handler with a new subscribe, it all seems to be working fine. But when I destroy the template from where I’m subscribing, and later I create it again (returning to a point in a single page application), it all starts to fail in the most strange of ways.

The first time I subscribe after re-creating the template, it all works fine. But when I stop the subscription, the client collection still has data. I’ve tested what I get after I stop() the handle (as you might have noticed, I do a console output to what the handle currently looks like after I stop it) in the publish method, and I’ve found this:

  1. Before destroying and re-creating the template, if I reset the handle (stop it and then create a new one), I this part of the handle JSON object:

    {
    _multiplexer : {
    _handles: null
    }
    }

and this happens all the time, no matter how many times I stop and re-create the handle.

  1. However, after I destroy and re-create the client template, the first time I stop the handle I get something like this in the _handles property that was previously null:

    {
    ‘32’: {
    _multiplexer: {
    _ordered: false,
    _onStop: [Function],
    _queue: [Object],
    _handles: [Circular],
    _readyFuture: [Object],
    _cache: [Object],
    _addHandleTasksScheduledButNotPerformed: 0,
    added: [Function],
    changed: [Function],
    removed: [Function],
    _observeDriver: [Object]
    },
    _added: [Function],
    _changed: [Function],
    _removed: [Function],
    _id: 32
    }
    }

As if the handle had another dependent handle that can’t be stopped. I don’t know why this happens, and more importantly, I haven’t been able to find out how to override this (maybe manually stop the dependent handle).

Any help would be greatly appreciated!

Your publish code looks fine. Can you please provide your subscribe code?